📄 subject_17037.htm
字号:
<p>
序号:17037 发表者:㊣客家人 发表日期:2002-10-07 20:46:37
<br>主题:VC调用AUTOCAD类型库acad.tlb时,一个三维点变量怎样转换成VARIANT类型?
<br>内容:VC调用AUTOCAD类型库acad.tlb时,一个三维点变量怎样转换成VARIANT类型?<BR><BR>例画圆函数AddCircle(const VARIANT& Center, double Radius)中的Center是怎样被赋值的?<BR>例如:我想让圆心坐标为(1,1,0),应怎样赋值呢?<BR>附代码<BR> IAcadApplication m_autocad;<BR> IAcadDocuments m_acaddocs;<BR> IAcadDocument m_acaddoc;<BR> IAcadModelSpace m_acadmodel;<BR><BR> m_autocad.CreateDispatch("AutoCAD.Application")<BR> m_autocad.SetVisible(true);<BR> m_acaddocs.AttachDispatch(m_autocad.GetDocuments(),true);<BR> m_acaddoc.AttachDispatch(m_acaddocs.Add(vtMissing),true);<BR> m_acadmodel.AttachDispatch(m_acaddoc.GetModelSpace(),true);<BR> //想画一个园<BR> VARIANT center;<BR> double radiu=100;<BR> //LPDISPATCH IAcadModelSpace::AddCircle(const VARIANT& Center, double Radius)<BR> //Center 应怎样赋值呢?<BR> m_acadmodel.AddCircle(certer,r);
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:㊣剑不如人♀ 回复日期:2002-10-07 20:50:40
<br>内容:我也很想知道耶!<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-08 09:03:14
<br>内容:acad.tlb 你把这个发给我,我看一下,我想他应该是把其(x,y,z)转化成一个以,分开的字符<BR>“x,y,z"(我裁猜的),如果猜对了,给点分,谢谢,如果不对给我发acad.tlb,分还挺多的
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:㊣剑不如人♀ 回复日期:2002-10-08 10:18:39
<br>内容:如你想得分,装一个autocad,acad.tlb在acad目录中<BR>我的程序是要调用autocad的,也只能在autocad中才能试出结果!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-08 16:13:49
<br>内容:Using Delphi 3 to write AutoCAD R14 Automation Clients<BR><BR>Here's an example which draws a circle using late binding<BR><BR>When using Late binding with the initial release of R14, you must use the VarArrayRef() function to pass a variant array when AutoCAD requires an array of 3 doubles (a 3D coordinate). In R14.01, you do not need to use VarArrayRef(), and you can just pass the variant array instead.<BR><BR>var<BR> Acad, Circle, vPoint, Mspace : OleVariant;<BR>begin<BR><BR> // Create the point array and assign values to it<BR> vPoint := VarArrayCreate([0,2], VT_R8);<BR> vPoint[0] := 2.0; vPoint[1] := 4.0; vPoint[2] := 0.0;<BR><BR> // Get the AutoCAD application object<BR> Acad := GetActiveOleObject('AutoCAD.Application.14');<BR><BR> // Get the ActiveDocument's model space object<BR> Mspace := Acad.ActiveDocument.Modelspace;<BR><BR> // call the AddCircle() method to create the <BR> // circle with a radius of 10 units:<BR><BR> Circle := Mspace.AddCircle(VarArrayRef(vPoint), 10.0); // R14.00 only<BR><BR> Circle := Mspace.AddCircle(Vpoint, 10.0); // R14.01 only<BR><BR> // Update the circle<BR> Circle.Update;<BR><BR>end;<BR><BR>Using early binding is a bit more complicated. Delphi has a bug that incorrectly declares SafeArray's as OleVariants in the imported type library (and generates a warning for each, but even the warning is incorrect - these parameters must be declared as const PSafearray (a constant pointer to a TSafeArray). So, for the AddCircle() method used in the above example, this would be the correct declaration that must appear in AutoCAD_TLB.pas:<BR><BR>function AddCircle(const center: PSafeArray; radius: Double): IDispatch; safecall;<BR><BR>Assuming you correct the declaration to read like the above, to use Early binding you must pass the safearray pointer. You can use this helper function to convert a variant array to a PSafeArray:<BR><BR>Function SafeArrayRef(V : OleVariant): PSafeArray;<BR>begin<BR> Result := PSafeArray(TVarData(V).VArray);|<BR>end;<BR><BR>Here is the same example from above, only using early binding:<BR><BR>var<BR> Acad, vPoint: OleVariant;<BR> Mspace : IAcadModelSpace;<BR> Circle : IAcadCircle;<BR>begin<BR> vPoint := VarArrayCreate([0,2], VT_R8);<BR> vPoint[0] := 2.0; vPoint[1] := 4.0; vPoint[2] := 0.0;<BR> Acad := GetActiveOleObject('AutoCAD.Application.14');<BR> Mspace := IDispatch(Acad.ActiveDocument.ModelSpace) as IAcadModelspace;<BR> Circle := IDispatch(Mspace.AddCircle(SafeArrayRef(vPoint), 10.0)) as IAcadCircle;<BR> Circle.Update;<BR>end;<BR>有一个delphi的例子
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-08 16:21:40
<br>内容:AutoCAD, Release 14 Java - nowe możliwości dla aplikacji<BR><BR>Listing 1. <BR>// import the library packages used so we can refer <BR>// to objects by their short names<BR>//<BR>import acad.*;<BR>import com.ms.com.*;<BR><BR>public class tryAcad<BR>{<BR>public static void main(String args[])<BR>{<BR>//<BR>// define the Application, Active Document and Model Space objects<BR>//<BR>IAcadApplication IApp = (IAcadApplication) new AcadApplication();<BR>IApp.putVisible(true);<BR>IAcadDocument IDoc = (IAcadDocument) IApp.getActiveDocument();<BR>IAcadUtility IUtil = (IAcadUtility) IDoc.getUtility();<BR>IAcadModelSpace IMSpace = (IAcadModelSpace) IDoc.getModelSpace();<BR>//<BR>// OK, here's where we would actually do something [insert code below]<BR>//<BR>}<BR>}<BR> <BR><BR> <BR><BR>Listing 2. <BR><BR>//<BR>// OK, so lets do something<BR>// Define two points and get user defined values<BR>//<BR>Variant opt = new Variant();<BR>opt.VariantClear();<BR>opt.noParam();<BR>Variant vPt1 = new Variant();<BR>Variant vPt2 = new Variant();<BR>vPt1.VariantClear();<BR>vPt2.VariantClear();<BR>int hr = 0;<BR>Variant prompt = new Variant();<BR>//<BR>// use 'try/catch' to get the point and handle exceptions, if thrown<BR>//<BR>try<BR>{<BR>prompt.putString("Enter starting point ===>");<BR>vPt1 = IUtil.GetPoint(opt, prompt);<BR>}<BR>catch (ComException e)<BR>{<BR>hr = e.getHResult();<BR>}<BR>if (hr == 0)<BR>{<BR>try<BR>{<BR>prompt.putString("Enter ending point ===>");<BR>vPt2 = IUtil.GetPoint(vPt1, prompt);<BR>}<BR>catch (ComException e)<BR>{<BR>hr = e.getHResult();<BR>}<BR>}<BR>//<BR>// draw a Line, a Circle and an Arc<BR>// and give them some colors<BR>//<BR>IAcadLine ILine;<BR>IAcadCircle ICir;<BR>IAcadArc IArc;<BR>if (hr == 0)<BR>{<BR>ILine = (IAcadLine) IMSpace.AddLine(vPt1, vPt2);<BR>ILine.putColor(2);<BR>ICir = (IAcadCircle) IMSpace.AddCircle(vPt1, 2.5);<BR>ICir.putColor(3);<BR>IArc = (IAcadArc) IMSpace.AddArc(vPt2, 2.5, 0.707, 1.414);<BR>IArc.putColor(4);<BR>}<BR>// All done<BR> <BR><BR>Tłumaczył: Wojtek Szypuła<BR><BR><BR><BR>给一个Java的例子
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:㊣剑不如人♀ 回复日期:2002-10-08 17:26:36
<br>内容:呵呵,C++BUILDER的程序我也有,和DELPHI的差不多<BR>可是VC的我试了半天也不行,可能我对safearray和variant概念还没理解透!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:㊣剑不如人♀ 回复日期:2002-10-08 18:37:23
<br>内容:HRESULT getDblArrayFromVariant(double pt[3], const VARIANT* pVal)<BR>{<BR> if (!(pVal->vt & VT_ARRAY && pVal->vt & VT_R8))<BR> return E_INVALIDARG;<BR><BR> SAFEARRAY* sPt = pVal->parray;<BR><BR> if (SafeArrayGetDim(sPt) != 1)<BR> return E_INVALIDARG;<BR><BR> long lLbound;<BR> long lUbound;<BR> SafeArrayGetLBound(sPt, 1, &lLbound);<BR> SafeArrayGetUBound(sPt, 1, &lUbound);<BR><BR> if ((lUbound - lLbound + 1) != 3)<BR> return E_INVALIDARG;<BR> <BR> HRESULT hr;<BR> for (long i = 0; i < 3; i++)<BR> if ((hr = SafeArrayGetElement(sPt, &i, &pt[i]))!=S_OK)<BR> return hr;<BR><BR> return S_OK;<BR>}<BR>//<BR>//<BR>//<BR>HRESULT getVariantFromDblArray(VARIANT* pVal, const double pt[3])<BR>{<BR> pVal->vt = VT_ARRAY | VT_R8;<BR><BR> SAFEARRAYBOUND rgsaBound;<BR> rgsaBound.lLbound = 0L;<BR> rgsaBound.cElements = 3;<BR><BR> pVal->parray = SafeArrayCreate(VT_R8, 1, &rgsaBound);<BR> if (! pVal->parray)<BR> return E_OUTOFMEMORY;<BR><BR> HRESULT hr;<BR> for (long i = 0; i < 3; i++)<BR> if ((hr = SafeArrayPutElement(pVal->parray, &i,<BR>(void*)&pt[i]))!=S_OK)<BR> return hr;<BR> return S_OK;<BR>}<BR>这试过了,这个答案是对的,拿分来!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -