高手看看,哪里的问题?
<p>Public Sub P_Length()<br/> Dim acadObj As Object<br/> Dim pline As AcadPolyline<br/> Dim plineCopy As AcadPolyline<br/> Dim explodedObjects As Variant<br/> Dim lineObj As AcadLine<br/> Dim length, length_Object As Double<br/> length = 0#<br/> length_Object = 0#<br/> For Each acadObj In ThisDrawing.ModelSpace<br/> If acadObj.ObjectName = "AcDb2dPolyline" Then<br/> 'If acadObj.ObjectName = "AcDbPolyline" Then<br/> Set pline = acadObj<br/> Set plineCopy = pline.Copy()<br/> explodedObjects = plineCopy.Explode<br/> Dim i As Integer<br/> For i = 0 To UBound(explodedObjects)<br/> Set lineObj = explodedObjects(i)<br/> length = length + lineObj.length<br/> length_Object = length_Object + lineObj.length<br/> explodedObjects(i).Delete<br/> Next<br/> ThisDrawing.Utility.Prompt "长度="& CStr(length_Object) & (Chr(13) & Chr(10))<br/> plinecoye.Delete<br/> End If<br/> length_Object = 0#<br/> Next acadObj<br/> ThisDrawing.Utility.Prompt "总长度="& CStr(length)<br/>End Sub<br/></p><p>问题: If acadObj.ObjectName = "AcDb2dPolyline" Then '使用此判断,判断结果一直为false<br/> If acadObj.ObjectName = "AcDbPolyline" Then '使用此判断,判断结果为真,但<br/> Set pline = acadObj '运行时出现,类型不匹配</p><p>以上代码,我是直接从书本中COPY来的。</p><p>不知道如何修改才能运行正常。</p><p>希望各位大侠帮忙。本人刚开始学这个!</p><p>谢谢谢谢谢谢了~</p><p></p><p></p><p></p> 没人回答么? <p>声明为Dim pline As AcadLWPolyline,其他做相应的改动</p> <p>Public Sub P_Length()<br/> Dim acadObj As Object<br/> Dim pline As AcadLWPolyline<br/> Dim plineCopy As AcadLWPolyline<br/> Dim aa As AcadLWPolyline<br/> Dim explodedObjects As Variant<br/> Dim lineObj As AcadLine<br/> Dim length, length_Object As Double<br/> length = 0#<br/> length_Object = 0#<br/> For Each acadObj In ThisDrawing.ModelSpace<br/> <br/> If acadObj.ObjectName = "AcDbPolyline" Then<br/> 'If acadObj.ObjectName = "AcDbPolyline" Then<br/> Set pline = acadObj<br/> Set plineCopy = pline.Copy()<br/> explodedObjects = plineCopy.Explode<br/> plineCopy.Delete</p><p> Dim i As Integer<br/> For i = 0 To UBound(explodedObjects)<br/> Set lineObj = explodedObjects(i)<br/> length = length + lineObj.length<br/> length_Object = length_Object + lineObj.length<br/> explodedObjects(i).Delete<br/> Next<br/> ThisDrawing.Utility.Prompt "长度=" & CStr(length_Object) & (Chr(13) & Chr(10))<br/> <br/> End If<br/> length_Object = 0#<br/> Next acadObj<br/> ThisDrawing.Utility.Prompt "总长度=" & CStr(length)<br/>End Sub</p><p></p><p></p><p>你的plineCopy.Delete也写错了。</p> <p>可以了/</p><p>谢谢谢谢,万分感谢中</p> <p>又一个问题</p><p>想将程序改成,当前模型空间内选中的多段线的总长度,怎么更改一下?</p><p>For Each acadObj In ThisDrawing.ModelSpace '</p><p>改成 For Each acadObj In ThisDrawing.SelectionSets 好象根本不对</p><p>新手新手,问题有点幼稚,不要笑话~~~</p> <p>For Each acadObj In ThisDrawing.PickfirstSelectionSet</p><p>不过用前看看置顶的“先选择后执行”的帖子</p> <p>谢谢~~~~</p><p>OK了</p> <p>Public Sub P_Length()<br/> Dim acadObj As Object<br/> Dim pline As AcadLWPolyline<br/> Dim plineCopy As AcadLWPolyline '优化多段线对象<br/> Dim explodedObjects As Variant '分解优化多段线,分解成多条直线<br/> Dim lineObj As AcadLine '直线对象<br/> Dim arcObj As AcadArc '圆弧对象<br/> Dim length, length_Object As Double<br/> length = 0#<br/> length_Object = 0#<br/> Dim ObjName As String<br/> 'For Each acadObj In ThisDrawing.ModelSpace<br/> For Each acadObj In ThisDrawing.PickfirstSelectionSet '获取选择优先的选择集<br/> ObjName = acadObj.ObjectName '新增,获取选中的对象的名称<br/> Select Case ObjName<br/> Case "AcDbPolyline" '情况1,对象为多段线 。原始代码<br/> Set pline = acadObj<br/> Set plineCopy = pline.Copy()<br/> explodedObjects = plineCopy.Explode<br/> plineCopy.Delete<br/> Dim i As Integer<br/> For i = 0 To UBound(explodedObjects)<br/> Set lineObj = explodedObjects(i) '多段线中第i条线段的长度<br/> length_Object = length_Object + lineObj.length '该多段线,所有线段长度累加<br/> length = length + lineObj.length '所有对象总长度累加<br/> explodedObjects(i).Delete<br/> Next<br/> ThisDrawing.Utility.Prompt "多段线长度=" & CStr(length_Object) & (Chr(13) & Chr(10))<br/> Case "AcDbLine" '情况2,对象为直线。新增代码<br/> Set lineObj = acadObj<br/> length_Object = lineObj.length '直线长度<br/> length = length + lineObj.length '所有对象总长度累加<br/> ThisDrawing.Utility.Prompt "直线长度=" & CStr(length_Object) & (Chr(13) & Chr(10))<br/> Case "AcDbArc"<br/> Set arcObj = acadObj<br/> length_Object = arcObj.ArcLength '圆弧长度<br/> length = length + arcObj.ArcLength '所有对象总长度累加<br/> ThisDrawing.Utility.Prompt "圆弧长度=" & CStr(length_Object) & (Chr(13) & Chr(10))<br/> End Select<br/> length_Object = 0#<br/> Next acadObj<br/> ThisDrawing.Utility.Prompt "所选中对象总长度=" & CStr(length)</p><p>End Sub</p> 多段线可以直接用他的长度属性。在多段线中可能存在圆弧。
页:
[1]