CAD 与VB 数据的结合,高手进!
请教大侠,我想利用VB程序提取CAD图形(简单图形)的各顶点的坐标,苦于知识浅薄,请不吝赐教。谢谢。 Sub Start()<br/> On Error Resume Next<br/> <br/> Dim Sel As AcadSelectionSet '选择集<br/> Dim Obj As AcadObject '<br/> Dim Sxyh As Variant<br/> Dim Exyh As Variant<br/> Dim Coord As Variant<br/> Dim XYZ(2) As Double<br/> Dim Js As Long<br/> <br/> Open "C:\XYH.dat" For Output As #1<br/> <br/> '错误处理<br/> Set Sel = ThisDrawing.SelectionSets("ss")<br/> If Err Then Set Sel = ThisDrawing.SelectionSets.Add("ss")<br/> <br/> '初始状态下清空选择集<br/> Sel.Clear<br/> Sel.SelectOnScreen<br/> <br/> For Each Obj In Sel<br/> If Obj.ObjectName = "AcDbLine" Then<br/> Js = Js + 1<br/> Sxyh = Obj.StartPoint<br/> Exyh = Obj.EndPoint<br/> 'XYZ_P_C Sxyh '屏幕坐标转测量坐标<br/> Print #1, Str(Js); " , "; Str(Js); " , "; Sxyh(1); " , "; Sxyh(0); " , "; Sxyh(2)<br/> Js = Js + 1<br/> 'XYZ_P_C Exyh<br/> Print #1, Str(Js); " , "; Str(Js); " , "; Exyh(1); " , "; Exyh(0); " , "; Exyh(2)<br/> ElseIf Obj.ObjectName = "AcDbPolyline" Or Obj.ObjectName = "AcDb2dPolyline" Then<br/> Coord = Obj.Coordinates<br/> XYZ(2) = Obj.Elevation<br/> For i = 0 To UBound(Coord) Step 3<br/> Js = Js + 1<br/> XYZ(1) = Coord(i + 1)<br/> XYZ(0) = Coord(i)<br/> 'XYZ_P_C XYZ<br/> Print #1, Str(Js); " , "; Str(Js); " , "; XYZ(1); " , "; XYZ(0); " , "; XYZ(2)<br/> Next i<br/> ElseIf Obj.ObjectName = "AcDb3dPolyline" Or Obj.ObjectName = "AcDbSpline" Then<br/> Coord = Obj.Coordinates<br/> For i = 0 To UBound(Coord) Step 3<br/> Js = Js + 1<br/> XYZ(1) = Coord(i + 1)<br/> XYZ(0) = Coord(i)<br/> XYZ(2) = Coord(i + 2)<br/> 'XYZ_P_C XYZ<br/> Print #1, Str(Js); " , "; Str(Js); " , "; XYZ(1); " , "; XYZ(0); " , "; XYZ(2)<br/> Next i<br/> End If<br/> Js = 0<br/> Next<br/> Close<br/>End Sub<br/> <p>楼上理解错误,他说的是VB程序打开cad,而非vba</p><p>打开VB,添加一个窗体,加上如下代码,然后运行,也可以编译成exe再运行</p><p>这段代码的作用是用VB程序打开dwg文件并写一行文字到模型空间,然后保存</p><p>Private Sub Form_Load()</p><p>On Error Resume Next<br/>Dim acadapp As object<br/>Set acadapp = CreateObject("AutoCAD.application")<br/>acadapp.Visible = False '是否显示<br/>Dim doc As object<br/>Set doc = acadapp.Documents.Open("c:/test.dwg") '打开文档<br/>doc.Activate<br/>Dim insertpt(2) As Double<br/>insertpt(0) = 0: insertpt(1) = 0: insertpt(2) = 0<br/>doc.ModelSpace.AddText "用VB打开AutoCAD例子程序", insertpt, 5<br/>acadapp.ZoomAll<br/>doc.Close True '并闭并保存<br/>acadapp.Quit '退出cad<br/>End Sub</p><p></p><p></p><p></p><p></p>
页:
[1]