明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1521|回复: 2

练手示例:给每个直线顶点加序号

[复制链接]
发表于 2008-6-25 12:12:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2008-6-26 12:52:22 编辑

这是一个练手的小示例。有兴趣的各位大侠可以引伸到零件标注号等小程序,还有其它的什么要求,大家都提出来,玩玩.
  1. Function CreatSelectionSet(InputEntityObjectName As Variant, Pt1 As Variant, Pt2 As Variant) As AcadSelectionSet
  2.    
  3.    On Error Resume Next
  4.    Dim SSet As AcadSelectionSet
  5.    If Not IsNull(ThisDrawing.SelectionSets.Item("SelectEntity")) Then
  6.      Set CreatSelectionSet = ThisDrawing.SelectionSets.Item("SelectEntity")
  7.      CreatSelectionSet.Delete
  8.    End If
  9.    Set CreatSelectionSet = ThisDrawing.SelectionSets.Add("SelectEntity")
  10.    'Pt1 = ThisDrawing.Utility.GetPoint(, "Input First Point")
  11.    'Pt2 = ThisDrawing.Utility.GetPoint(Pt1, "Input First Point")
  12.    
  13.    Dim gpCode(0) As Integer
  14.    Dim dataValue(0) As Variant
  15.    'ReDim dataValue(UBound(InputEntityObjectName)) As Variant
  16.    gpCode(0) = 0
  17.    
  18.    For ii = 0 To UBound(InputEntityObjectName)
  19.      dataValue(ii) = InputEntityObjectName(ii)
  20.    Next ii
  21.    CreatSelectionSet.Select acSelectionSetWindow, Pt1, Pt2, gpCode, dataValue
  22. End Function
  23. Sub ReadTable()
  24.    Dim Pt1(0 To 2) As Double, Pt2(0 To 2) As Double
  25.    Dim SSet As AcadSelectionSet
  26.    Dim InputEntityObjectName As Variant
  27.    Pt1(0) = 2850: Pt1(1) = 2660: Pt1(2) = 0
  28.    Pt2(0) = -10: Pt2(1) = -10: Pt2(2) = 0
  29.    InputEntityObjectName = Array("Line", "Text", "Dimension")
  30.    
  31.    Set SSet = CreatSelectionSet(InputEntityObjectName, Pt1, Pt2)
  32.    Debug.Print SSet.Count
  33.    Dim Ent As AcadEntity, DrawingText As AcadText
  34.    Dim DrawingLine As AcadLine, DrawingCircle As AcadCircle
  35.    ii = 1
  36.     Dim alignmentPoint(0 To 2) As Double
  37.     alignmentPoint(0) = 5: alignmentPoint(1) = 3: alignmentPoint(2) = 0
  38.    
  39.    For Each DrawingLine In SSet
  40.       With ThisDrawing.ModelSpace
  41.         Set DrawingCircle = .AddCircle(DrawingLine.EndPoint, 35)
  42.         Set DrawingText = .AddText(ii, alignmentPoint, 30)
  43.         With DrawingText
  44.           '.HorizontalAlignment = acHorizontalAlignmentFit
  45.           .Alignment = acAlignmentMiddleCenter
  46.           .TextAlignmentPoint = DrawingLine.EndPoint
  47.          
  48.         End With
  49.       End With
  50.       ii = ii + 1
  51.    Next
  52. End Sub
方法1
  1. Sub ll()
  2.   Dim LineData As AcadLine, ArcData As AcadArc
  3.   Dim DrawingText As AcadText, DrawingCircle As AcadCircle
  4.   Close #1
  5.   Open "D:\ls.txt" For Output As #1
  6.   
  7.   Write #1, "m1", "m2", "m3", "m4", "m5", "m6", "m7", "m8", "m9", "m10", "m11", "m12"
  8.   
  9.   Dim Ent As AcadEntity
  10.   ii = 1
  11.   For Each Ent In ThisDrawing.ModelSpace
  12.             
  13.    
  14.     m1 = Ent.ObjectName
  15.     m2 = Ent.ObjectID
  16.     Select Case Ent.ObjectName
  17.       Case "AcDbLine"
  18.         Set LineData = Ent
  19.         
  20.         With LineData
  21.           Set DrawingCircle = ThisDrawing.ModelSpace.AddCircle(.StartPoint, 35)
  22.          
  23.           Set DrawingText = ThisDrawing.ModelSpace.AddText(ii, .EndPoint, 30)
  24.           With DrawingText
  25.             .Alignment = acAlignmentMiddleCenter
  26.             .TextAlignmentPoint = LineData.StartPoint
  27.             ii = ii + 1
  28.           End With
  29.          
  30.           m3 = Round(.StartPoint(0), 5)
  31.           m4 = Round(.StartPoint(1), 5)
  32.           m5 = Round(.StartPoint(2), 5)
  33.           m6 = Round(.EndPoint(0), 5)
  34.           m7 = Round(.EndPoint(1), 5)
  35.           m8 = Round(.EndPoint(2), 5)
  36.          
  37.         End With
  38.     End Select
  39.     Write #1, m1, m2, m3, m4, m6, m7, m8
  40.    
  41.   Next Ent
  42.   
  43.   Close #1
  44. End Sub

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2008-6-25 14:13:00 | 显示全部楼层
取每个端点的坐标,并输出!
 楼主| 发表于 2008-6-29 09:20:00 | 显示全部楼层
以下程序将点送到Excel
  1. Sub ll()
  2.    ThisDrawing.ActiveTextStyle.fontFile = "c:\windows\fonts\SIMHEI.TTF"
  3.    Dim xlsSheet As Worksheet
  4.    Set xlsSheet = ReturnXlsSheet(1)
  5.    xlsSheet.Range("a:z").ClearContents
  6.    
  7.   
  8.    Dim LineData As AcadLine, ArcData As AcadArc
  9.    Dim DrawingText As AcadText, DrawingCircle As AcadCircle
  10.    Close #1
  11.    Open "D:\ls.txt" For Output As #1
  12.    
  13.    Write #1, "m1", "m2", "m3", "m4", "m5", "m6", "m7", "m8", "m9", "m10", "m11", "m12"
  14.    
  15.    Dim Ent As AcadEntity
  16.    'Debug.Print ThisDrawing.ModelSpace.Count
  17.    For ii = 1 To ThisDrawing.ModelSpace.Count
  18.      'm1 = Ent.ObjectName
  19.      Set Ent = ThisDrawing.ModelSpace.Item(ii - 1)
  20.      Debug.Print ii, Ent.ObjectName, Ent.Handle
  21.      m2 = Ent.ObjectID
  22.      Select Case Ent.ObjectName
  23.        Case "AcDbLine"
  24.          Set LineData = Ent
  25.          With LineData
  26.            'Set DrawingCircle = ThisDrawing.ModelSpace.AddCircle(.StartPoint, 35)
  27.            m1 = "第" & ii & "点"
  28.      If ii = 1 Then
  29.            m3 = Round(.StartPoint(0), 2)
  30.            m4 = Round(.StartPoint(1), 2)
  31.      Else
  32.        m3 = "=c" & ii - 1 & "+ i" & ii - 1
  33.        m4 = "=d" & ii - 1 & "+ j" & ii - 1
  34.      End If
  35.            m5 = Round(.StartPoint(2), 2)
  36.            'm6 = Round(.EndPoint(0), 2)
  37.     If ii = ThisDrawing.ModelSpace.Count Then
  38.            m6 = "=c1"
  39.            m7 = "=d1"
  40.    Else
  41.            m6 = "=c" & ii & "+ i" & ii
  42.            'm7 = Round(.EndPoint(1), 2)
  43.            m7 = "=d" & ii & "+ j" & ii
  44.    
  45.    End If
  46.            m8 = Round(.EndPoint(2), 2)
  47.            m9 = .Delta(0)
  48.            m10 = .Delta(1)
  49.            m11 = .Delta(2)
  50.            ttt = "第" & ii & "点 " & "(" & m3 & "," & m4 & ")"
  51.            
  52.            'Set DrawingText = ThisDrawing.ModelSpace.AddText(ttt, .EndPoint, 10)
  53.            With DrawingText
  54.             ' .Alignment = acAlignmentMiddleCenter
  55.             ' .TextAlignmentPoint = LineData.StartPoint
  56.             
  57.              'ii = ii + 1
  58.            End With
  59.            
  60.          End With
  61.      End Select
  62.      Write #1, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11 ', m12
  63.      
  64.      With xlsSheet
  65.        .Cells(ii, 1) = m1
  66.        .Cells(ii, 2) = m2
  67.        .Cells(ii, 3) = m3
  68.        .Cells(ii, 4) = m4
  69.        .Cells(ii, 5) = m5
  70.        .Cells(ii, 6) = m6
  71.        .Cells(ii, 7) = m7
  72.        .Cells(ii, 8) = m8
  73.        .Cells(ii, 9) = m9
  74.        .Cells(ii, 10) = m10
  75.        .Cells(ii, 11) = m11
  76.      End With
  77.    Next ii
  78.    
  79.    Close #1
  80. End Sub
  81. Function ReturnXlsSheet(InputSheetNum As Integer) As Worksheet
  82.      Dim xlApp As Object     ' This Line ,Not set Excel , run Excel
  83.      ' 发生错误时跳到下一个语句继续执行
  84.      On Error Resume Next
  85.      ' 连接Excel应用程序
  86.      Set xlApp = GetObject(, "Excel.Application")
  87.      
  88.      If Err.Number <> 0 Then
  89.          Set xlApp = CreateObject("Excel.Application")
  90.          xlApp.Visible = True
  91.          xlApp.Workbooks.Add
  92.      End If
  93.      ' 返回当前活动的工作表
  94.      Set ReturnXlsSheet = xlApp.Sheets(InputSheetNum)
  95. End Function
  96. Sub gggg()
  97.    Dim xlsSheet As Worksheet
  98.    Set xlsSheet = ReturnXlsSheet(1)
  99.    Dim pp(0 To 2) As Double, ppp(0 To 2) As Double
  100.    Dim ll As AcadLine
  101.    For ii = 1 To 8
  102.      For jj = 0 To 2
  103.        pp(jj) = xlsSheet.Cells(ii, jj + 3)
  104.        ppp(jj) = xlsSheet.Cells(ii, jj + 6)
  105.      Next jj
  106.      Set ll = ThisDrawing.ModelSpace.AddLine(pp, ppp)
  107.      ll.color = ii
  108.    Next ii
  109.    
  110. End Sub
  111. Sub ls()
  112.    Dim xlsSheet As Worksheet
  113.    Set xlsSheet = ReturnXlsSheet(1)
  114.    xlsSheet.Range("a:z").ClearContents
  115.   
  116.   Dim EntCount As Integer
  117.   Dim Ent As AcadEntity, lineObj As AcadLine
  118.   EntCount = ThisDrawing.ModelSpace.Count
  119.   Dim mm() As Long
  120.   Dim Num As Integer
  121.   ReDim mm(EntCount - 2) As Long
  122.   Dim BaseStartPoint As Variant, BaseEndPoint As Variant
  123.   Num = 0
  124.   For ii = 0 To EntCount - 1
  125.     Set lineObj = ThisDrawing.ModelSpace(ii)
  126.     With lineObj
  127.       If .color = 200 Then
  128.         baseentity = .ObjectID
  129.         If .StartPoint(0) < .EndPoint(0) Then
  130.           BaseStartPoint = lineObj.StartPoint
  131.           BaseEndPoint = lineObj.EndPoint
  132.         Else
  133.           BaseStartPoint = lineObj.EndPoint
  134.           BaseEndPoint = lineObj.StartPoint
  135.         End If
  136.         For jj = 0 To 2
  137.           With xlsSheet
  138.              .Cells(2, jj + 1) = BaseStartPoint(jj)
  139.              .Cells(2, jj + 1 + 3) = BaseEndPoint(jj)
  140.              .Cells(2, 7) = "第1点"
  141.           End With
  142.         Next jj
  143.       Else
  144.         mm(Num) = lineObj.ObjectID
  145.         Num = Num + 1
  146.       End If
  147.     End With
  148.    
  149.   Next ii
  150.   Dim mmm() As Long  ', ssmm As Long
  151.   ReDim mmm(EntCount - 2) As Long
  152. nn = 3
  153.   For ii = 0 To EntCount - 2
  154.     'ssmm = mm(ii)
  155.     Num = 0
  156.     For jj = 0 To EntCount - 2
  157.       If baseentity <> mm(jj) Then
  158.         mmm(Num) = mm(jj)
  159.         Num = Num + 1
  160.       End If
  161.     Next jj
  162.     Dim llss As Variant
  163.     llss = InputPointData(BaseEndPoint, mmm)
  164.     BaseEndPoint = llss(1)
  165.         For jj = 0 To 2
  166.           With xlsSheet
  167.             .Cells(nn, jj + 1) = llss(0)(jj)
  168.             .Cells(nn, jj + 1 + 3) = llss(1)(jj)
  169.             .Cells(nn, 7) = "第" & nn - 1 & "点"
  170.           End With
  171.         Next jj
  172.    
  173.     nn = nn + 1
  174.   Next ii
  175. End Sub
  176. Function InputPointData(InputPoint, InputArray) As Variant()
  177.   Dim Ent As AcadEntity, lineObj As AcadLine
  178.   Dim pp(1) As Variant, ppp(0 To 2) As Variant
  179.   For ii = 0 To UBound(InputArray)
  180.     Set lineObj = ThisDrawing.ObjectIdToObject(InputArray(ii))
  181.     If InputPoint(0) = lineObj.EndPoint(0) _
  182.        And InputPoint(1) = lineObj.EndPoint(1) _
  183.        And InputPoint(2) = lineObj.EndPoint(2) Then
  184.       
  185.        pp(0) = lineObj.EndPoint
  186.        pp(1) = lineObj.StartPoint
  187.       
  188.        InputPointData = pp
  189.        Exit Function
  190.     End If
  191.     If InputPoint(0) = lineObj.StartPoint(0) _
  192.       And InputPoint(1) = lineObj.StartPoint(1) _
  193.       And InputPoint(2) = lineObj.StartPoint(2) Then
  194.       
  195.          pp(0) = lineObj.StartPoint
  196.          pp(1) = lineObj.EndPoint
  197.          InputPointData = pp
  198.          Exit Function
  199.     End If
  200.   Next ii
  201. End Function
  202. Function InputEntityReturnType(InputChar As Long, InputArray As Variant)
  203.   Dim Ent As AcadEntity, Ent1 As AcadEntity
  204.   Dim pp()
  205.   ReDim pp(UBound(InputArray) - 1)
  206.   Dim BaseCount As Integer
  207.   Set Ent = ThisDrawing.ObjectIdToObject(InputChar)
  208.   BaseCount = 0
  209.   For ii = 0 To UBound(InputArray)
  210.     Set Ent1 = ThisDrawing.ObjectIdToObject(InputArray(ii))
  211.     pp(BaseCount) = Ent.IntersectWith(Ent1, acExtendNone)
  212.     BaseCount = BaseCount + 1
  213.   Next ii
  214. End Function
  215. Sub DrawingCircle()
  216.    Dim xlsSheet As Worksheet
  217.    Set xlsSheet = ReturnXlsSheet(1)
  218.    Dim pp(0 To 2) As Double, ppp(0 To 2) As Double
  219.    Dim ll As AcadLine
  220.    Dim DefineCircle As AcadCircle, TextObj As AcadText
  221.    For ii = 2 To 6
  222.      For jj = 0 To 2
  223.        pp(jj) = xlsSheet.Cells(ii, jj + 1)
  224.        ppp(jj) = xlsSheet.Cells(ii, jj + 4)
  225.      Next jj
  226.      Set DefineCircle = ThisDrawing.ModelSpace.AddCircle(pp, 25)
  227.      Set TextObj = ThisDrawing.ModelSpace.AddText(ii - 1, pp, 20)
  228.      
  229.      With TextObj
  230.         .Alignment = acAlignmentMiddleCenter
  231.         .TextAlignmentPoint = DefineCircle.Center
  232.      End With
  233.      DefineCircle.color = ii
  234.    Next ii
  235.    
  236. End Sub
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-26 09:54 , Processed in 0.183039 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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