- UID
- 262220
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2005-5-19
- 最后登录
- 1970-1-1
|
发表于 2005-5-21 03:16:22
|
显示全部楼层
自己编的小程序
'这个小程序是自己用VB6编的(稍作调整可用于VBA),用来标注坐标(适用于规划、测量学,XY坐标互换),最后将文字与引线合并为一个匿名组。里面有些是全局变量,有的可能忘记修改了,有的没有赋值,用的时候仔细修改一遍才行。
- public acadApp As Object
- public acadDoc As Object
- public utilObj As Object
- Public moSpace As Object
-
- Private Sub cmdLeader_Click()
- Dim vattmpPnt1 As Variant '标注点
- Dim vattmpPnt2 As Variant '起始点
- Dim strPntXYFormatted(0 To 1) As String '格式化以后的XY坐标,字符串
- Dim blnRightDirection As Boolean
- Dim dblLeaderPLineVertex(0 To 5) As Double '标注引线顶点坐标
- Dim dblOriginPnt(0 To 2) As Double '坐标原点
- Dim dblStartPnt(0 To 2) As Double
- Dim dblEndpnt(0 To 2) As Double
-
- Dim vatPntMin As Variant
- '包围盒左下角坐标,用于确定文字长度,否则引线长度与引线不匹配
- Dim vatPntMax As Variant
- Dim objBlock As Object
- Dim objPLine As Object
- Dim objAtt As Object
-
- Dim blnFoundIt As Boolean
- Dim vatAttVars As Variant
- Dim dbltmpTH As Double '标注文字高度
-
- Dim objPntGroup As Object '组对象
- Dim objtmpArray(0 To 1) As Object '加入到组的所有对象
- dim strFormat as string '坐标文本格式
- Const dblconTextRiseRatio As Double = 0.3! '引线上部坐标向上便宜数量
- Const dblconTextFallRatio As Double = -1.3!
- Const strConstLeaderBlock As String = "BLK_Leader" '图块名称
- '可以不用图块,不过用图块有其优越的地方,特别是以后自己的软件需要对其读写的时候
- On Error Resume Next
- Set acadApp = GetObject(, "AutoCAD.Application.14")
- If Err Then
- Err.Clear
- Set acadApp = CreateObject("AutoCAD.Application.14")
- End If
- Set acadDoc = acadApp.ActiveDocument
- Set utilObj = acadDoc.Utility
- Set moSpace = acadDoc.ModelSpace
- strFormat="###0.00" '坐标格式化样式,可以把123表示为123.00
- Me.Hide
-
- dbltmpTH = MdblTextHeight * (MdblPlotRatio) 'MdblTextHeight是1:1时字高,全局变量,需要另外赋值,
- 'MdblPlotRatio为图纸比例,全局变量,需要另外赋值,dbltmpTH为实际字高,也可以根据自己的要求设置
- dblOriginPnt(0) = 0#
- dblOriginPnt(1) = 0#
- dblOriginPnt(2) = 0#
- dblStartPnt(2) = 0#
- dblEndpnt(2) = 0#
-
- blnFoundIt = False
- For Each objBlock In acadDoc.Blocks
- If StrComp(UCase(objBlock.Name), UCase(strConstLeaderBlock), 1) = 0 Then
- blnFoundIt = True
- Exit For
- End If
- Next objBlock '查找有没有图块"BLK_LEADER"
-
- MstrKeyWordList = "X eXit"
- Do While 1
- Call ChangeOSNAP(True) '打开捕捉模式
- utilObj.InitializeUserInput 1, MstrKeyWordList
- vattmpPnt1 = utilObj.GetPoint(, "Start point (eXit) :")
-
- If Err Then
- Err.Clear
- If StrComp(Err.Description, "User input is a keyword", 1) = 0 Then
- MstrInputString = utilObj.GetInput
- If UCase(MstrInputString) = "X" Or MstrInputString = "" Then GoTo theEnd
- Else
- GoTo theEnd
- End If
- End If
-
- Call ChangeOSNAP(False) '关闭捕捉模式
- utilObj.InitializeUserInput 1, MstrKeyWordList
- vattmpPnt2 = utilObj.GetPoint(vattmpPnt1, "To point (eXit) :")
- If Err Then
- Err.Clear
- If StrComp(Err.Description, "User input is a keyword", 1) = 0 Then
- MstrInputString = utilObj.GetInput
- If UCase(MstrInputString) = "X" Or MstrInputString = "" Then GoTo theEnd
- Else
- GoTo theEnd
- End If
- End If
-
- blnRightDirection = (vattmpPnt2(0) >= vattmpPnt1(0)) '确定引线标注的标注方向
- strPntXYFormatted(1) = "Y=" & Format(Trim(Round((vattmpPnt1(0)), MlngCorBit)), strFormat)
- '标注坐标Y,MlngCorBit:全局变量,另外赋值,保留的小数位数
- strPntXYFormatted(0) = "X=" & Format(Trim(Round((vattmpPnt1(1)), MlngCorBit)), strFormat)
- '标注坐标X
- If Not blnFoundIt Then '图形文件中如果没有“BLK_LEADER”则生成该土块
- Set objBlock = acadDoc.Blocks.Add(dblOriginPnt, strConstLeaderBlock)
- dblStartPnt(0) = dblOriginPnt(0)
- dblStartPnt(1) = dblOriginPnt(1) + dblconTextRiseRatio * dbltmpTH
- Set objAtt = objBlock.AddAttribute(dbltmpTH, acAttributeModePreset, "1", dblStartPnt, _
- "1", strPntXYFormatted(0))
-
- dblStartPnt(1) = dblOriginPnt(1) + dblconTextFallRatio * dbltmpTH
- Set objAtt = objBlock.AddAttribute(dbltmpTH, acAttributeModePreset, "1", dblStartPnt, _
- "1", strPntXYFormatted(1))
- blnFoundIt = True
- End If
-
- dblStartPnt(0) = vattmpPnt1(0)
- dblStartPnt(1) = vattmpPnt1(1)
- Set objBlock = moSpace.InsertBlock(dblStartPnt, strConstLeaderBlock, 1, 1, 0) '插入图块
-
- vatAttVars = objBlock.GetAttributes
- dblStartPnt(0) = vattmpPnt2(0) + 0.1 * dbltmpTH
- dblStartPnt(1) = vattmpPnt2(1) + dblconTextRiseRatio * dbltmpTH
- vatAttVars(0).insertionPoint = dblStartPnt
- vatAttVars(0).TextString = strPntXYFormatted(0)
- vatAttVars(0).Height = dbltmpTH
- '调整Y坐标(属性文字)位置、文字内容、字高。VBA插入图块时属性文字位置、文字内容不会随块变化
-
- dblStartPnt(1) = vattmpPnt2(1) + dblconTextFallRatio * dbltmpTH
- vatAttVars(1).insertionPoint = dblStartPnt
- vatAttVars(1).TextString = strPntXYFormatted(1)
- vatAttVars(1).Height = dbltmpTH
- '调整X坐标(属性文字)位置、文字内容、字高。
-
- Call objBlock.GetBoundingBox(vatPntMin, vatPntMax) '取包围属性文字矩形框的左下角及右上角坐标
- If Not blnRightDirection Then
- dblEndpnt(0) = dblStartPnt(0) - vatPntMax(0) + vatPntMin(0)
- dblEndpnt(1) = dblStartPnt(1)
-
- dblStartPnt(0) = vattmpPnt2(0) + vatPntMin(0) - vatPntMax(0)
- dblStartPnt(1) = vattmpPnt2(1) + dblconTextRiseRatio * dbltmpTH
- vatAttVars(0).insertionPoint = dblStartPnt
-
- dblStartPnt(1) = vattmpPnt2(1) + dblconTextFallRatio * dbltmpTH
- vatAttVars(1).insertionPoint = dblStartPnt
- End If
-
- dblLeaderPLineVertex(0) = vattmpPnt1(0)
- dblLeaderPLineVertex(1) = vattmpPnt1(1)
- dblLeaderPLineVertex(2) = vattmpPnt2(0)
- dblLeaderPLineVertex(3) = vattmpPnt2(1)
- dblLeaderPLineVertex(4) = IIf(blnRightDirection, vatPntMax(0), dblEndpnt(0) - 0.1 * dbltmpTH)
- dblLeaderPLineVertex(5) = vattmpPnt2(1)
- Set objPLine = moSpace.AddLightWeightPolyline(dblLeaderPLineVertex)
- Set objPntGroup = acadDoc.Groups.Add("*")
- Set objtmpArray(0) = objBlock
- Set objtmpArray(1) = objPLine
- objPntGroup.AppendItems objtmpArray
- Loop
- theEnd:
- Me.Show
- End Sub
- Function ChangeOSNAP(mySwith As Boolean)
- Dim vattmpRetValue As Variant
- vattmpRetValue = acadDoc.GetVariable("OSMODE")
- If vattmpRetValue = 0 Then vattmpRetValue = 167
- If mySwith Then
- vattmpRetValue = IIf((vattmpRetValue <= 4095), vattmpRetValue, vattmpRetValue - 16384)
- Else
- vattmpRetValue = IIf((vattmpRetValue <= 4095), vattmpRetValue + 16384, vattmpRetValue)
- End If
- Call acadDoc.SetVariable("OSMODE", vattmpRetValue)
- End Function '切换捕捉模式,如果没有设置,则设置捕捉端点、中点、圆心、交点、垂足
|
|