明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: cxf11991

还是直线问题

  [复制链接]
发表于 2004-6-4 22:12:00 | 显示全部楼层
cxf11991发表于2004-6-4 17:11:00我做的是关于剪切线段的代码 比如说有4条直线成井字放置,长短都不一样,我想把交点之间的线段都剪掉,
这样:
  1. Option ExplicitPublic Sub CrossWall()
  2. Dim hline1 As AcadLine, hline2 As AcadLine, vline1 As AcadLine, vline2 As AcadLine
  3. Dim hline11 As AcadLine, hline12 As AcadLine
  4. Dim hline21 As AcadLine, hline22 As AcadLine
  5. Dim vline11 As AcadLine, vline12 As AcadLine
  6. Dim vline21 As AcadLine, vline22 As AcadLine
  7. Dim p1, p2, p3, p4 As Variant
  8. Dim sset As AcadSelectionSet
  9. Dim scount As Integer, i As Integer   i = ThisDrawing.SelectionSets.Count
  10.    
  11.    While (i > 0)
  12.            Set sset = ThisDrawing.SelectionSets.Item(i - 1)
  13.            If sset.Name = "EXTSET" Then
  14.                    sset.Delete
  15.            End If
  16.            i = i - 1
  17.    Wend
  18.    
  19.    Set sset = ThisDrawing.SelectionSets.Add("EXTSET")   ThisDrawing.Utility.Prompt "请「框选」欲修十字交角的墙线....."
  20. Dim cwp1, cwp2, interpt
  21.    
  22.    cwp1 = ThisDrawing.Utility.GetPoint(, "框选的第一点:")
  23.    cwp2 = ThisDrawing.Utility.GetCorner(cwp1, "框选的对角点:")
  24.    
  25. Dim gpCode(0) As Integer
  26. Dim dataValue(0) As Variant
  27.    gpCode(0) = 0
  28.    dataValue(0) = "Line"
  29.    
  30. Dim groupCode As Variant, dataCode As Variant
  31.    groupCode = gpCode
  32.    dataCode = dataValue
  33.    
  34.    sset.Select acSelectionSetCrossing, cwp1, cwp2, groupCode, dataCode   scount = sset.Count
  35.       
  36.    If scount > 4 Then
  37.        MsgBox "您框选了超过四条以上的墙线. 请再执行程序一次....", vbOKOnly, "trim error"
  38.        Exit Sub
  39.    ElseIf scount < 4 Then
  40.        MsgBox "您框选了的墙线少于四条. 请再执行程序一次....", vbOKOnly, "trim error"
  41.        Exit Sub
  42.    End If
  43.    
  44.    
  45.    Set hline1 = sset.Item(0)
  46.    
  47.    Dim si As Integer
  48.    
  49.    For si = 1 To 3
  50.        interpt = hline1.IntersectWith(sset.Item(si), acExtendNone)
  51.        If UBound(interpt) = -1 Then
  52.            Set hline2 = sset.Item(si)
  53.        End If
  54.    Next
  55.    
  56.    For si = 1 To 3
  57.        If sset.Item(si).ObjectID <> hline1.ObjectID And sset.Item(si).ObjectID <> hline2.ObjectID Then
  58.            Set vline1 = sset.Item(si)
  59.        End If
  60.    Next
  61.    For si = 1 To 3
  62.        If sset.Item(si).ObjectID <> hline1.ObjectID And _
  63.        sset.Item(si).ObjectID <> hline2.ObjectID And sset.Item(si).ObjectID <> vline1.ObjectID Then
  64.            Set vline2 = sset.Item(si)
  65.        End If
  66.    Next
  67.    
  68.    p1 = hline1.IntersectWith(vline1, acExtendNone)
  69.    p2 = hline1.IntersectWith(vline2, acExtendNone)
  70.    p3 = hline2.IntersectWith(vline1, acExtendNone)
  71.    p4 = hline2.IntersectWith(vline2, acExtendNone)
  72.    If UBound(p1) = -1 Or UBound(p2) = -1 Or UBound(p3) = -1 Or UBound(p4) = -1 Then
  73.        MsgBox "您所选取的墙线无法在 X 方向做截取动作,请于修正错误后,再试一次!", vbOKOnly
  74.        Exit Sub
  75.    End If
  76.    
  77.    If distance(hline1.StartPoint, p1) > distance(hline1.StartPoint, p2) Then
  78.        Set hline11 = ThisDrawing.ModelSpace.AddLine(hline1.StartPoint, p2)
  79.        Set hline12 = ThisDrawing.ModelSpace.AddLine(hline1.EndPoint, p1)
  80.    Else
  81.        Set hline11 = ThisDrawing.ModelSpace.AddLine(hline1.StartPoint, p1)
  82.        Set hline12 = ThisDrawing.ModelSpace.AddLine(hline1.EndPoint, p2)
  83.    End If
  84.    
  85.    If distance(hline2.StartPoint, p3) > distance(hline1.StartPoint, p4) Then
  86.        Set hline21 = ThisDrawing.ModelSpace.AddLine(hline2.StartPoint, p4)
  87.        Set hline22 = ThisDrawing.ModelSpace.AddLine(hline2.EndPoint, p3)
  88.    Else
  89.        Set hline21 = ThisDrawing.ModelSpace.AddLine(hline2.StartPoint, p3)
  90.        Set hline22 = ThisDrawing.ModelSpace.AddLine(hline2.EndPoint, p4)
  91.    End If
  92.    
  93.    If distance(vline1.StartPoint, p1) > distance(vline1.StartPoint, p3) Then
  94.        Set vline11 = ThisDrawing.ModelSpace.AddLine(vline1.StartPoint, p3)
  95.        Set vline12 = ThisDrawing.ModelSpace.AddLine(vline1.EndPoint, p1)
  96.    Else
  97.        Set vline11 = ThisDrawing.ModelSpace.AddLine(vline1.StartPoint, p1)
  98.        Set vline12 = ThisDrawing.ModelSpace.AddLine(vline1.EndPoint, p3)
  99.    End If
  100.    
  101.    If distance(vline2.StartPoint, p2) > distance(vline2.StartPoint, p4) Then
  102.        Set vline21 = ThisDrawing.ModelSpace.AddLine(vline2.StartPoint, p4)
  103.        Set vline22 = ThisDrawing.ModelSpace.AddLine(vline2.EndPoint, p2)
  104.    Else
  105.        Set vline21 = ThisDrawing.ModelSpace.AddLine(vline2.StartPoint, p2)
  106.        Set vline22 = ThisDrawing.ModelSpace.AddLine(vline2.EndPoint, p4)
  107.    End If
  108.    
  109.    hline11.Layer = hline1.Layer
  110.    hline12.Layer = hline1.Layer
  111.    hline21.Layer = hline1.Layer
  112.    hline22.Layer = hline1.Layer
  113.    vline11.Layer = hline1.Layer
  114.    vline12.Layer = hline1.Layer
  115.    vline21.Layer = hline1.Layer
  116.    vline22.Layer = hline1.Layer
  117.    
  118.    hline1.Delete
  119.    hline2.Delete
  120.    vline1.Delete
  121.    vline2.Delete
  122. End Sub
  123. Function distance(pt1, pt2 As Variant) As Double
  124.        distance = ((pt1(0) - pt2(0)) ^ 2 + (pt1(1) - pt2(1)) ^ 2) ^ 0.5
  125. End Function
 楼主| 发表于 2004-6-5 10:22:00 | 显示全部楼层
发贴心情 谢谢大家,我昨天重新做了一次,发现了几个错误,现在的程序是可行的,有兴趣的可以看看
我做的是关于剪切线段的代码 比如说有4条直线成井字放置,长短都不一样, 我想把交点之间的线段都剪掉, 编了下面部分代码
flog = True
For Each entobj3 In objselectionset1
For Each entobj4 In objselectionset4
Set lineobj = entobj3
pt = entobj3.IntersectWith(entobj4, acExtendNone)
If flog = True Then
set lineobj1 = ThisDrawing.ModelSpace.AddLine(lineobj.StartPoint, pt)
set lineobj2 = ThisDrawing.ModelSpace.AddLine(lineobj.EndPoint, pt)
flog = False
Else
set lineobj3 = ThisDrawing.ModelSpace.AddLine(lineobj.StartPoint, pt)
set lineobj4 = ThisDrawing.ModelSpace.AddLine(lineobj.EndPoint, pt)

If Sqr((lineobj1.StartPoint(0) - lineobj1.EndPoint(0)) ^ 2 + (lineobj1.StartPoint(1) - lineobj1.EndPoint(1)) ^ 2) < Sqr((lineobj3.StartPoint(0) - lineobj3.EndPoint(0)) ^ 2 + (lineobj3.StartPoint(1) - lineobj3.EndPoint(1)) ^ 2) Then
lineobj3.Delete

Else: lineobj1.Delete
End If
If Sqr((lineobj2.StartPoint(0) - lineobj2.EndPoint(0)) ^ 2 + (lineobj2.StartPoint(1) - lineobj2.EndPoint(1)) ^ 2) < Sqr((lineobj4.StartPoint(0) - lineobj4.EndPoint(0)) ^ 2 + (lineobj4.StartPoint(1) - lineobj4.EndPoint(1)) ^ 2) Then
lineobj4.Delete
Else: lineobj2.Delete
End If
flog = True
End If
Next
entobj3.Delete
Next
其中objselectionset1为两条横放直线,objselectionset2为两条竖放直线
这样就可以剪切两条相交直线了,另外两条直线剪切原理一样,选择集反一下,就可以了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-28 05:31 , Processed in 0.140881 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表