yydpj 发表于 2008-3-24 14:04:00

高手看看,哪里的问题?

<p>Public Sub P_Length()<br/>&nbsp; Dim acadObj As Object<br/>&nbsp; Dim pline As AcadPolyline<br/>&nbsp; Dim plineCopy As AcadPolyline<br/>&nbsp; Dim explodedObjects As Variant<br/>&nbsp; Dim lineObj As AcadLine<br/>&nbsp; Dim length, length_Object As Double<br/>&nbsp; length = 0#<br/>&nbsp; length_Object = 0#<br/>&nbsp; For Each acadObj In ThisDrawing.ModelSpace<br/>&nbsp;&nbsp;&nbsp; If acadObj.ObjectName = "AcDb2dPolyline" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp; 'If acadObj.ObjectName = "AcDbPolyline" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set pline = acadObj<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set plineCopy = pline.Copy()<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; explodedObjects = plineCopy.Explode<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim i As Integer<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = 0 To UBound(explodedObjects)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set lineObj = explodedObjects(i)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length = length + lineObj.length<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length_Object = length_Object + lineObj.length<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; explodedObjects(i).Delete<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ThisDrawing.Utility.Prompt "长度="&amp; CStr(length_Object) &amp; (Chr(13) &amp; Chr(10))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; plinecoye.Delete<br/>&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp; length_Object = 0#<br/>&nbsp; Next acadObj<br/>&nbsp; ThisDrawing.Utility.Prompt&nbsp;&nbsp;"总长度="&amp; CStr(length)<br/>End Sub<br/></p><p>问题:&nbsp;If acadObj.ObjectName = "AcDb2dPolyline" Then&nbsp;&nbsp;'使用此判断,判断结果一直为false<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If acadObj.ObjectName = "AcDbPolyline" Then&nbsp;&nbsp;&nbsp;'使用此判断,判断结果为真,但<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Set pline = acadObj&nbsp;&nbsp;&nbsp; '运行时出现,类型不匹配</p><p>以上代码,我是直接从书本中COPY来的。</p><p>不知道如何修改才能运行正常。</p><p>希望各位大侠帮忙。本人刚开始学这个!</p><p>谢谢谢谢谢谢了~</p><p></p><p></p><p></p>

yydpj 发表于 2008-3-24 16:35:00

没人回答么?

雪山飞狐_lzh 发表于 2008-3-24 18:55:00

<p>声明为Dim pline As AcadLWPolyline,其他做相应的改动</p>

fjfhgdwfn 发表于 2008-3-25 10:29:00

<p>Public Sub P_Length()<br/>&nbsp; Dim acadObj As Object<br/>&nbsp; Dim pline As AcadLWPolyline<br/>&nbsp; Dim plineCopy As AcadLWPolyline<br/>&nbsp; Dim aa As AcadLWPolyline<br/>&nbsp; Dim explodedObjects As Variant<br/>&nbsp; Dim lineObj As AcadLine<br/>&nbsp; Dim length, length_Object As Double<br/>&nbsp; length = 0#<br/>&nbsp; length_Object = 0#<br/>&nbsp; For Each acadObj In ThisDrawing.ModelSpace<br/>&nbsp;<br/>&nbsp;&nbsp;&nbsp; If acadObj.ObjectName = "AcDbPolyline" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp; 'If acadObj.ObjectName = "AcDbPolyline" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set pline = acadObj<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set plineCopy = pline.Copy()<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; explodedObjects = plineCopy.Explode<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; plineCopy.Delete</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim i As Integer<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = 0 To UBound(explodedObjects)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set lineObj = explodedObjects(i)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length = length + lineObj.length<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length_Object = length_Object + lineObj.length<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; explodedObjects(i).Delete<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ThisDrawing.Utility.Prompt "长度=" &amp; CStr(length_Object) &amp; (Chr(13) &amp; Chr(10))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp; length_Object = 0#<br/>&nbsp; Next acadObj<br/>&nbsp; ThisDrawing.Utility.Prompt "总长度=" &amp; CStr(length)<br/>End Sub</p><p></p><p></p><p>你的plineCopy.Delete也写错了。</p>

yydpj 发表于 2008-3-25 11:37:00

<p>可以了/</p><p>谢谢谢谢,万分感谢中</p>

yydpj 发表于 2008-3-25 14:19:00

<p>又一个问题</p><p>想将程序改成,当前模型空间内选中的多段线的总长度,怎么更改一下?</p><p>For Each acadObj In ThisDrawing.ModelSpace&nbsp; '</p><p>改成&nbsp;&nbsp; For Each acadObj In ThisDrawing.SelectionSets 好象根本不对</p><p>新手新手,问题有点幼稚,不要笑话~~~</p>

雪山飞狐_lzh 发表于 2008-3-25 20:28:00

<p>For Each acadObj In ThisDrawing.PickfirstSelectionSet</p><p>不过用前看看置顶的“先选择后执行”的帖子</p>

yydpj 发表于 2008-3-26 08:19:00

<p>谢谢~~~~</p><p>OK了</p>

yydpj 发表于 2008-3-26 09:27:00

<p>Public Sub P_Length()<br/>&nbsp; Dim acadObj As Object<br/>&nbsp; Dim pline As AcadLWPolyline<br/>&nbsp; Dim plineCopy As AcadLWPolyline '优化多段线对象<br/>&nbsp; Dim explodedObjects As Variant '分解优化多段线,分解成多条直线<br/>&nbsp; Dim lineObj As AcadLine '直线对象<br/>&nbsp; Dim arcObj As AcadArc&nbsp;&nbsp; '圆弧对象<br/>&nbsp; Dim length, length_Object As Double<br/>&nbsp; length = 0#<br/>&nbsp; length_Object = 0#<br/>&nbsp; Dim ObjName As String<br/>&nbsp; 'For Each acadObj In ThisDrawing.ModelSpace<br/>&nbsp; For Each acadObj In ThisDrawing.PickfirstSelectionSet&nbsp; '获取选择优先的选择集<br/>&nbsp;&nbsp;&nbsp;&nbsp; ObjName = acadObj.ObjectName&nbsp;&nbsp; '新增,获取选中的对象的名称<br/>&nbsp;&nbsp;&nbsp;&nbsp; Select Case ObjName<br/>&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbPolyline"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '情况1,对象为多段线 。原始代码<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set pline = acadObj<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set plineCopy = pline.Copy()<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; explodedObjects = plineCopy.Explode<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; plineCopy.Delete<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim i As Integer<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = 0 To UBound(explodedObjects)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set lineObj = explodedObjects(i)&nbsp;&nbsp; '多段线中第i条线段的长度<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length_Object = length_Object + lineObj.length&nbsp; '该多段线,所有线段长度累加<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length = length + lineObj.length&nbsp;&nbsp; '所有对象总长度累加<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; explodedObjects(i).Delete<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ThisDrawing.Utility.Prompt "多段线长度=" &amp; CStr(length_Object) &amp; (Chr(13) &amp; Chr(10))<br/>&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbLine"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '情况2,对象为直线。新增代码<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set lineObj = acadObj<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length_Object = lineObj.length&nbsp; '直线长度<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length = length + lineObj.length&nbsp;&nbsp; '所有对象总长度累加<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ThisDrawing.Utility.Prompt "直线长度=" &amp; CStr(length_Object) &amp; (Chr(13) &amp; Chr(10))<br/>&nbsp;&nbsp;&nbsp;&nbsp; Case "AcDbArc"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set arcObj = acadObj<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length_Object = arcObj.ArcLength '圆弧长度<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length = length + arcObj.ArcLength&nbsp;&nbsp; '所有对象总长度累加<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ThisDrawing.Utility.Prompt "圆弧长度=" &amp; CStr(length_Object) &amp; (Chr(13) &amp; Chr(10))<br/>&nbsp;&nbsp;&nbsp;&nbsp; End Select<br/>&nbsp;&nbsp;&nbsp;&nbsp; length_Object = 0#<br/>&nbsp; Next acadObj<br/>&nbsp; ThisDrawing.Utility.Prompt "所选中对象总长度=" &amp; CStr(length)</p><p>End Sub</p>

fjfhgdwfn 发表于 2008-3-26 16:09:00

多段线可以直接用他的长度属性。在多段线中可能存在圆弧。
页: [1]
查看完整版本: 高手看看,哪里的问题?