[原创]曲线打断于交点
<CommandMethod("TlsSB")> Public Shared Sub TlsSelectionSetBreak()Dim ss As TlsSelectionSet<BR> Dim objIds As ObjectIdCollection<BR> Dim objs As New ObjectIdCollection<BR> Dim i, j As ObjectId<BR> Dim k As Point3d<BR> Dim oCurve, pCurve As Curve<BR> Dim pnts, dots As Point3dCollection<BR> Dim pTM As New TlsTM
pTM.StartTrans()<BR> Try<BR> pTM.OpenBlockTableRecord(BlockTableRecord.ModelSpace)
ss = New TlsSelectionSet("TlsSel")<BR> ss.SetFilter(0, "Line,Arc,Circle,Ellipse,Spline,Lwpolyline")<BR> ss.SelectObjectOnScreen()<BR> objIds = ss.ToObjectIdCollection
For Each i In objIds<BR> pnts = New Point3dCollection<BR> oCurve = i.Open(OpenMode.ForRead, False, True)
For Each j In objIds<BR> dots = New Point3dCollection<BR> If i.OldId <> j.OldId Then<BR> pCurve = j.Open(OpenMode.ForRead, False, True)<BR> oCurve.IntersectWith(pCurve, Intersect.OnBothOperands, dots, 0, 0)<BR> For Each k In dots<BR> pnts.Add(k)<BR> Next<BR> pCurve.Close()<BR> End If<BR> Next
pTM.SortPnts(oCurve, pnts)<BR> If Not (oCurve.Closed And pnts.Count = 1) Then<BR> objs.Add(i)<BR> pTM.Add(oCurve.GetSplitCurves(pnts))<BR> End If<BR> oCurve.Close()<BR> Next
pTM.Remove(objs)
pTM.CommitTrans()<BR> Catch ex As Exception<BR> Finally<BR> pTM.Dispose()<BR> End Try
End Sub<BR> <BR>Imports Autodesk.AutoCAD.ApplicationServices<BR>Imports Autodesk.AutoCAD.DatabaseServices<BR>Imports Autodesk.AutoCAD.Runtime<BR>Imports Autodesk.AutoCAD.Interop<BR>Imports Autodesk.AutoCAD.Interop.Common<BR>Imports Autodesk.AutoCAD.Geometry
Public Class TlsSelectionSet<BR> Private m_oSel As AcadSelectionSet<BR> Private m_vFilterType() As Short, m_vFilterData() As Object<BR> Private m_sName As String<BR> Private m_oApp As AcadApplication = Application.AcadApplication<BR> Private m_oDoc As AcadDocument
Public Sub NullFilter()<BR> '清空过滤器
m_vFilterType = Nothing<BR> m_vFilterData = Nothing
End Sub
Private Function IsNull() As Boolean
If m_oSel Is Nothing Then<BR> IsNull = True<BR> ElseIf m_oSel.Count = 0 Then<BR> IsNull = True<BR> Else<BR> IsNull = False<BR> End If
End Function<BR> Public Sub New()<BR> Me.New("TlsSel")<BR> End Sub
Public Sub New(ByVal Name As String)<BR> m_oDoc = m_oApp.ActiveDocument<BR> Init(Name)<BR> End Sub<BR> Public Sub Init(ByVal Name As String)<BR> '创建选择集
NullFilter()<BR> If Not m_oSel Is Nothing Then m_oSel.Delete()<BR> m_sName = Name<BR> Try<BR> m_oDoc.SelectionSets.Item(m_sName).Delete()<BR> Catch ex As Exception
End Try<BR> m_oSel = m_oDoc.SelectionSets.Add(m_sName)
End Sub
Protected Overrides Sub Finalize()<BR> MyBase.Finalize()<BR> If Not m_oSel Is Nothing Then m_oSel.Delete()<BR> End Sub
Public ReadOnly Property Count() As Integer<BR> '获取选择集实体个数<BR> Get<BR> Count = m_oSel.Count<BR> End Get<BR> End Property
Public ReadOnly Property Name() As String<BR> '获取选择集名称<BR> Get<BR> Name = m_sName<BR> End Get<BR> End Property
Public ReadOnly Property Item(ByVal Index) As AcadEntity<BR> '获取选择集实体<BR> Get<BR> Item = m_oSel.Item(Index)<BR> End Get<BR> End Property
Public Sub AddItem(ByVal obj As AcadEntity)<BR> '向选择集加入单个实体
Dim objs(0) As AcadEntity
objs(0) = obj<BR> m_oSel.AddItems(objs)
End Sub
Public Sub AddItem(ByVal objs As AcadEntity())<BR> '向选择集加入实体数组
m_oSel.AddItems(objs)
End Sub
Public Sub RemoveItem(ByVal obj As AcadEntity)<BR> '在选择集中移除单个实体
Dim objs(0) As AcadEntity<BR> objs(0) = obj<BR> m_oSel.RemoveItems(objs)
End Sub
<BR> Public Sub RemoveItem(ByVal objs As AcadEntity())<BR> '在选择集中移除实体数组
m_oSel.RemoveItems(objs)
End Sub
Public Sub Clear()<BR> '清空选择集
Select Case m_sName<BR> Case "PICKFIRST"<BR> GetPickfirstSelectionSet()<BR> Case "CURRENT"<BR> GetActiveSelectionSet()<BR> Case Else<BR> Init(m_sName)<BR> End Select
m_oSel.Clear()
End Sub
Public Sub Update()
m_oSel.Update()
End Sub
Public Sub GetPickfirstSelectionSet()<BR> '获取Pickfirst选择集
NullFilter()<BR> If Not m_oSel Is Nothing Then m_oSel.Delete()<BR> m_sName = "PICKFIRST"<BR> m_oDoc.SelectionSets.Item(m_sName).Delete()<BR> m_oSel = m_oDoc.PickfirstSelectionSet
End Sub
Public Sub GetActiveSelectionSet()<BR> '获取Active选择集<BR> On Error Resume Next
NullFilter()<BR> If Not m_oSel Is Nothing Then m_oSel.Delete()<BR> m_sName = "CURRENT"<BR> m_oDoc.SelectionSets.Item(m_sName).Delete()<BR> m_oSel = m_oDoc.ActiveSelectionSet
End Sub
Public Sub SetFilterType(ByVal ParamArray FilterType())<BR> '设置过滤器类型
Dim i<BR> Dim nCount As Short<BR> nCount = UBound(FilterType)<BR> ReDim m_vFilterType(nCount)
For i = 0 To nCount<BR> m_vFilterType(i) = FilterType(i)<BR> Next i
End Sub
Public Sub SetFilterData(ByVal ParamArray FilterData())<BR> '设置过滤器数据
Dim i<BR> Dim nCount As Integer<BR> nCount = UBound(FilterData)<BR> ReDim m_vFilterData(nCount)
For i = 0 To nCount<BR> m_vFilterData(i) = FilterData(i)<BR> Next i
End Sub
Public Sub SetFilter(ByVal ParamArray Filter())<BR> '设置过滤器
Dim i<BR> Dim n As Integer<BR> Dim nCount As Integer<BR> nCount = (UBound(Filter) + 1) / 2 - 1<BR> ReDim m_vFilterType(nCount), m_vFilterData(nCount)
For i = 0 To nCount<BR> n = i * 2<BR> m_vFilterType(i) = Filter(n)<BR> m_vFilterData(i) = Filter(n + 1)<BR> Next i
End Sub
<BR> Public Sub SelectObjectOnScreen()
If IsArray(m_vFilterType) Then<BR> m_oSel.SelectOnScreen(m_vFilterType, m_vFilterData)<BR> Else<BR> m_oSel.SelectOnScreen()<BR> End If
End Sub
Public Sub SelectObject(ByVal Mode As AcSelect, ByVal Point1 As Object, ByVal Point2 As Object)
If IsArray(m_vFilterType) Then<BR> m_oSel.Select(Mode, Point1, Point2, m_vFilterType, m_vFilterData)<BR> Else<BR> m_oSel.Select(Mode, Point1, Point2)<BR> End If
End Sub<BR> Public Sub SelectObject(ByVal Mode As AcSelect)
If IsArray(m_vFilterType) Then<BR> m_oSel.Select(Mode, , , m_vFilterType, m_vFilterData)<BR> Else<BR> m_oSel.Select(Mode)<BR> End If
End Sub
Public Sub SelectObjectAtPoint(ByVal Point)<BR> On Error Resume Next
If IsArray(m_vFilterType) Then<BR> m_oSel.SelectAtPoint(Point, m_vFilterType, m_vFilterData)<BR> Else<BR> m_oSel.SelectAtPoint(Point)<BR> End If
End Sub
Public Sub SelectObjectByPolygon(ByVal Mode As AcSelect, ByVal Points As Object)
If IsArray(m_vFilterType) Then<BR> m_oSel.SelectByPolygon(Mode, Points, m_vFilterType, m_vFilterData)<BR> Else<BR> m_oSel.SelectByPolygon(Mode, Points)<BR> End If
End Sub
Public WriteOnly Property Visible() As Boolean<BR> Set(ByVal Value As Boolean)<BR> If IsNull() Then Exit Property
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.Visible = Value<BR> Next i<BR> End Set<BR> End Property
<BR> Public WriteOnly Property Layer() As String<BR> Set(ByVal Value As String)
If IsNull() Then Exit Property
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.Layer = Value<BR> Next i<BR> End Set<BR> End Property
Public WriteOnly Property LineType() As String<BR> Set(ByVal Value As String)
If IsNull() Then Exit Property
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.Linetype = Value<BR> Next i<BR> End Set<BR> End Property
Public WriteOnly Property Color() As ACAD_COLOR<BR> Set(ByVal Value As ACAD_COLOR)
If IsNull() Then Exit Property
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.color = Value<BR> Next i<BR> End Set<BR> End Property
Public Sub Move(ByVal Point1 As Object, ByVal Point2 As Object)
If IsNull() Then Exit Sub
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.Move(Point1, Point2)<BR> Next i
End Sub
Public Function Copy(ByVal Point1 As Object, ByVal Point2 As Object) As AcadEntity()
If IsNull() Then Exit Function
Dim objs() As AcadEntity<BR> Dim i<BR> ReDim objs(Count - 1)
For i = 0 To Count<BR> objs(i) = m_oSel.Item(i).Copy<BR> objs(i).Move(Point1, Point2)<BR> Next i
Return objs
End Function
Public Sub Rotate(ByVal BasePoint As Object, Optional ByVal RotationAngle As Double = 1.0#)
If IsNull() Then Exit Sub
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.Rotate(BasePoint, RotationAngle)<BR> Next i
End Sub
Public Sub Rotate3D(ByVal Point1 As Object, ByVal Point2 As Object, Optional ByVal RotationAngle As Double = 1.0#)
If IsNull() Then Exit Sub
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.Rotate3D(Point1, Point2, RotationAngle)<BR> Next i
End Sub
Public Sub ScaleAll(ByVal BasePoint As Object, Optional ByVal ScaleFactor As Double = 1)
If IsNull() Then Exit Sub
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.ScaleEntity(BasePoint, ScaleFactor)<BR> Next i
End Sub
Public Sub Mirror(ByVal Point1 As Object, ByVal Point2 As Object)
If IsNull() Then Exit Sub
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.Mirror(Point1, Point2)<BR> Next i
End Sub
Public Sub Mirror3D(ByVal Point1 As Object, ByVal Point2 As Object, ByVal Point3 As Object)
If IsNull() Then Exit Sub
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.Mirror3D(Point1, Point2, Point3)<BR> Next i
End Sub
Public Sub Highlight(Optional ByVal HighlightFlag As Boolean = True)
Dim i As AcadEntity<BR> For Each i In m_oSel<BR> i.Highlight(HighlightFlag)<BR> Next i
End Sub
Public Sub Delete()
m_oSel.Erase()
End Sub
<BR> Public Sub CopyObjects(ByVal Owner As Object, ByVal IdPairs As Object)
If IsNull() Then Exit Sub
m_oDoc.CopyObjects(ToArray, Owner, IdPairs)
End Sub
Public Sub CopyObjects(ByVal Owner As Object)
If IsNull() Then Exit Sub
m_oDoc.CopyObjects(ToArray, Owner)
End Sub
Public Sub CopyObjects()
If IsNull() Then Exit Sub
m_oDoc.CopyObjects(ToArray)
End Sub
<BR> Public Function GetBoundingBox(ByRef MinPoint As Object, ByRef MaxPoint As Object) As Boolean
Dim i<BR> Dim d1, d2, p1, p2
If IsNull() Then Exit Function
m_oSel.Item(0).GetBoundingBox(d1, d2)
For i = 1 To Count - 1
m_oSel.Item(i).GetBoundingBox(p1, p2)
If p1(0) < d1(0) Then d1(0) = p1(0)<BR> If p1(1) < d1(1) Then d1(1) = p1(1)<BR> If p2(0) > d2(0) Then d2(0) = p2(0)<BR> If p2(1) > d2(1) Then d2(1) = p2(1)
Next i
MinPoint = d1<BR> MaxPoint = d2
End Function
Public Function ToBlock(ByVal InsertionPoint As Object, Optional ByVal Name As String = "*U") As String
If IsNull() Then Exit Function
Dim oBlock As AcadBlock<BR> oBlock = m_oDoc.Blocks.Add(InsertionPoint, Name)<BR> CopyObjects(oBlock)
Return oBlock.Name
End Function
Public Function ToSelectionSet() As AcadSelectionSet<BR> '获取选择集
Return m_oSel
End Function
Public Function ToArray()<BR> '转化选择集为对象数组输出
Dim i<BR> Dim objs() As AcadEntity<BR> Dim nCount As Integer
nCount = m_oSel.Count - 1<BR> ReDim objs(nCount)
For i = 0 To nCount<BR> objs(i) = m_oSel.Item(i)<BR> Next i
Return objs
End Function
Public Function ToObjectIdCollection() As ObjectIdCollection<BR> '转化选择集为对象数组输出<BR> Dim i<BR> Dim objId As ObjectId<BR> Dim objs As New ObjectIdCollection<BR> For i = 0 To m_oSel.Count - 1<BR> objId.OldId = m_oSel.Item(i).ObjectID<BR> objs.Add(objId)<BR> Next i
Return objs
End Function
<BR>End Class<BR> Imports Autodesk.AutoCAD.ApplicationServices<BR>Imports Autodesk.AutoCAD.DatabaseServices<BR>Imports Autodesk.AutoCAD.Runtime<BR>Imports Autodesk.AutoCAD.Interop<BR>Imports Autodesk.AutoCAD.Geometry<BR>Imports AutoCadTM = Autodesk.AutoCAD.DatabaseServices.TransactionManager
Public Class TlsTM<BR> Private pDatabase As Database<BR> Private pTransactionManager As AutoCadTM<BR> Private pStartTransaction As Transaction<BR> Private pBlockTable As BlockTable<BR> Private pBlockTableRecord As BlockTableRecord
'程序功能:向当前块表记录中加入实体<BR> Public Function Add(ByVal TlsEntity As DBObject)
pBlockTableRecord.AppendEntity(TlsEntity)<BR> pTransactionManager.AddNewlyCreatedDBObject(TlsEntity, True)
End Function
'程序功能:向当前块表记录中加入实体数组<BR> Public Function Add(ByVal TlsEntity As DBObject())
Dim i As DBObject<BR> For Each i In TlsEntity<BR> Add(i)<BR> Next i
End Function
Public Function Add(ByVal TlsEntity As DBObjectCollection)
Dim i As DBObject<BR> For Each i In TlsEntity<BR> Add(i)<BR> Next i
End Function
Public Sub Remove(ByVal ObjId As ObjectId)
Dim oEntity As Entity<BR> oEntity = ObjId.Open(OpenMode.ForWrite, True, True)<BR> oEntity.Erase(True)<BR> oEntity.Close()
End Sub
Public Sub Remove(ByVal ObjIds As ObjectIdCollection)
Dim i As ObjectId<BR> For Each i In ObjIds<BR> Remove(i)<BR> Next
End Sub
'程序功能:生成一个新块,并加入实体<BR> Public Function AddBlock(ByVal Name As String, ByVal Entitys As DBObject()) As ObjectId<BR> Dim i As DBObject<BR> Dim pDatabase As Database = Application.DocumentManager.MdiActiveDocument.Database<BR> Dim pTransactionManager As AutoCadTM = pDatabase.TransactionManager<BR> Dim pStartTransaction As Transaction = pTransactionManager.StartTransaction()<BR> Try
Dim pBlockTable As BlockTable = CType(pTransactionManager.GetObject(pDatabase.BlockTableId, OpenMode.ForWrite, False), BlockTable)<BR> Dim pBlockTableRecord As New BlockTableRecord<BR> pBlockTableRecord.Name = Name<BR> pBlockTable.Add(pBlockTableRecord)<BR> Dim pId As ObjectId = pBlockTableRecord.Id
For Each i In Entitys<BR> pBlockTableRecord.AppendEntity(i)<BR> pTransactionManager.AddNewlyCreatedDBObject(i, True)<BR> Next i
pBlockTableRecord.Close()<BR> pBlockTable.Close()<BR> pStartTransaction.Commit()
Return pId
Finally<BR> pStartTransaction.Dispose()<BR> End Try
End Function
'开始事务<BR> Public Sub StartTrans()
pDatabase = Application.DocumentManager.MdiActiveDocument.Database<BR> pTransactionManager = pDatabase.TransactionManager<BR> pStartTransaction = pTransactionManager.StartTransaction()
End Sub
'打开一个块表记录<BR> Public Sub OpenBlockTableRecord(ByVal str As String)
pBlockTable = CType(pTransactionManager.GetObject(pDatabase.BlockTableId, OpenMode.ForRead, False), BlockTable)<BR> pBlockTableRecord = CType(pTransactionManager.GetObject(pBlockTable(str), OpenMode.ForWrite, False), BlockTableRecord)
End Sub
'事务提交<BR> Public Sub CommitTrans()
pBlockTableRecord.Close()<BR> pBlockTable.Close()<BR> pStartTransaction.Commit()
End Sub
'事务结束<BR> Public Sub Dispose()
pStartTransaction.Dispose()<BR> pBlockTableRecord = Nothing<BR> pBlockTable = Nothing<BR> pStartTransaction = Nothing<BR> pTransactionManager = Nothing<BR> pDatabase = Nothing
End Sub
<BR> '获取当前的辅助工具<BR> Public Function Utility() As AcadUtility
Return Application.AcadApplication.ActiveDocument.Utility
End Function
'曲线上的点排序<BR> Public Sub SortPnts(ByVal TlsCurve As Curve, ByRef TlsPnts As Point3dCollection)
Dim i, j As Short<BR> Dim nCount As Short<BR> Dim pTmp As Point3d<BR> nCount = TlsPnts.Count<BR> For i = 1 To nCount - 1<BR> For j = 0 To nCount - i - 1<BR> Try<BR> If TlsCurve.GetDistAtPoint(TlsPnts(j)) > TlsCurve.GetDistAtPoint(TlsPnts(j + 1)) Then<BR> pTmp = TlsPnts(j)<BR> TlsPnts.RemoveAt(j)<BR> TlsPnts.Insert(j + 1, pTmp)<BR> End If<BR> Catch ex As Exception<BR> End Try<BR> Next<BR> Next
End Sub
<BR> Public Function AddLine(ByVal pointer1() As Double, ByVal pointer2() As Double) As Line
Dim pLine As Line<BR> pLine = New Line( _<BR> New Point3d(pointer1(0), pointer1(1), pointer1(2)), _<BR> New Point3d(pointer2(0), pointer2(1), pointer2(2)))
Add(pLine)<BR> Return pLine
End Function<BR>End Class<BR>
页:
[1]