实际上他们的objectid的值是一样的,但是却是两种不同的类型
Function NetID2VBAID(ByVal ID As ObjectId) As Integer
Dim I As Integer
Dim Str As String
Str = ID.ToString
I = Val(Str.Substring(1, Str.Length - 2))
NetID2VBAID = I
End Function
Dim I As Integer
Dim Str As String
Str = ID.ToString
I = Val(Str.Substring(1, Str.Length - 2))
NetID2VBAID = I
End Function
然后使用
ThisDrawing.ObjectIdToObject(Id)
就得到AcadEntity
比如我们使用:.Net 在AutoCAD 2006 中,返回对象的角点,使用GeomExtents.MaxPoint和GeomExtents.MinPoint对于文字对象和填充对象,得到这个点是错的。
这时候我还是习惯地想起了VBA
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Module VBA_Mod
Dim ThisDrawing As AcadDocument = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication.ActiveDocument
Function P2P_AngleByVBA(ByVal P1 As Object, ByVal P2 As Object) As Double
P2P_AngleByVBA = ThisDrawing.Utility.AngleFromXAxis(P1, P2)
End Function
'返回对象中心点
Function GetEntCenter1(ByVal ID As ObjectId) As point3d
Dim VBA_ID As Integer
VBA_ID = NetID2VBAID(ID)
Dim E As AcadEntity = ThisDrawing.ObjectIdToObject(VBA_ID)
Dim MinP As Object = Nothing, MaxP As Object = Nothing
E.GetBoundingBox(MinP, MaxP)
GetEntCenter1 = New Point3d((MinP(0) + MaxP(0)) / 2, _
(MinP(1) + MaxP(1)) / 2, _
(MinP(2) + MaxP(2)) / 2)
End Function
End Module
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Module VBA_Mod
Dim ThisDrawing As AcadDocument = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication.ActiveDocument
Function P2P_AngleByVBA(ByVal P1 As Object, ByVal P2 As Object) As Double
P2P_AngleByVBA = ThisDrawing.Utility.AngleFromXAxis(P1, P2)
End Function
'返回对象中心点
Function GetEntCenter1(ByVal ID As ObjectId) As point3d
Dim VBA_ID As Integer
VBA_ID = NetID2VBAID(ID)
Dim E As AcadEntity = ThisDrawing.ObjectIdToObject(VBA_ID)
Dim MinP As Object = Nothing, MaxP As Object = Nothing
E.GetBoundingBox(MinP, MaxP)
GetEntCenter1 = New Point3d((MinP(0) + MaxP(0)) / 2, _
(MinP(1) + MaxP(1)) / 2, _
(MinP(2) + MaxP(2)) / 2)
End Function
End Module
而且我们还可以使用AngleFromXAxis返回点到点的角度,省的我们自己用向量几何去判断这个角度。
[本日志由 tiancao1001 于 2009-12-31 10:09 AM 编辑]
|
tiancao1001 于 2010-01-21 05:08 PM 发表评论:
Public Function P2P_Angle(ByVal PT1 As Point3d, ByVal PT2 As Point3d) As Double
'Pt1为坐标原点
Dim DX As Double
Dim DY As Double
DX = PT1.X - PT2.X
DY = PT1.Y - PT2.Y
Dim A45 As Double = Math.Atan(1) '45度角
If DX = 0 Then '垂直线
If DY < 0 Then
P2P_Angle1 = A45 * 2 '90度
P2P_Angle1 = A45 * 6 '270
End If
ElseIf DX < 0 Then
If DY <= 0 Then
P2P_Angle1 = Math.Atan(DY / DX) '第一象限(包含x轴正方向0度)
Else
P2P_Angle1 = A45 * 8 - Math.Atan(-DY / DX) '第四象限
End If
ElseIf DX > 0 Then
P2P_Angle1 = Math.Atan(DY / DX) + A45 * 4 '第二、第三象限(包含x轴负方向180度)
End If
End Function
'Pt1为坐标原点
Dim DX As Double
Dim DY As Double
DX = PT1.X - PT2.X
DY = PT1.Y - PT2.Y
Dim A45 As Double = Math.Atan(1) '45度角
If DX = 0 Then '垂直线
If DY < 0 Then
P2P_Angle1 = A45 * 2 '90度
P2P_Angle1 = A45 * 6 '270
End If
ElseIf DX < 0 Then
If DY <= 0 Then
P2P_Angle1 = Math.Atan(DY / DX) '第一象限(包含x轴正方向0度)
Else
P2P_Angle1 = A45 * 8 - Math.Atan(-DY / DX) '第四象限
End If
ElseIf DX > 0 Then
P2P_Angle1 = Math.Atan(DY / DX) + A45 * 4 '第二、第三象限(包含x轴负方向180度)
End If
End Function
发表评论 - 不要忘了输入验证码哦! |