如何从椭圆修剪线条(AutoCAD 2014)

时间:2014-04-08 15:42:28

标签: vb.net winforms line trim autocad

目标

修剪椭圆中的一条直线到达它的中心。应该修剪进入椭圆的线条部分。

这是未修剪的椭圆

Untrimmed

这是修剪椭圆,这个问题的目标

Trimmed


尝试

CreateConveyorNameEllipse(AcadDoc)

Public Function CreateConveyorNameEllipse(ByRef AcadDoc As Document) As ObjectId
    Dim returnId As ObjectId
    Dim db As Database = AcadDoc.Database
    Dim x As Vector3d = db.Ucsxdir
    Dim y As Vector3d = db.Ucsydir
    Dim normalVec As Vector3d = x.CrossProduct(y)
    Dim axisvec As Vector3d = normalVec.GetNormal()
    Dim CenterPoint As New Point3d(Me.StartPoint.X + 50, Me.StartPoint.Y + 40, 0)
    Dim aEllipse As Ellipse

    aEllipse = New Ellipse(CenterPoint, axisvec, New Vector3d(30, 0, 0), 0.35, 0, Math.PI * 2)
    aEllipse.SetDatabaseDefaults()
    returnId = Utils.CreateAcadObject(AcadDoc, aEllipse)

    aEllipse.Dispose()
    Utils.regenLayers()

    Return returnId
End Function

CreateConveyorEllipseLineConnection(AcadDoc)

Public Function CreateConveyorEllipseLineConnection(ByRef AcadDoc As Document) As ObjectId
    Dim returnId As ObjectId
    Dim CenterPoint As New Point3d(Me.StartPoint.X + 50, Me.StartPoint.Y + 40, 0)
    Dim aLine As Line

    aLine = New Line(Me.StartPoint, CenterPoint)
    aLine.SetDatabaseDefaults()
    returnId = Utils.CreateAcadObject(AcadDoc, aLine)

    aLine.Dispose()
    Utils.regenLayers()

    Return returnId
End Function

CreateAcadObject(AcadDoc,AcadObj)

Public Function CreateAcadObject(ByRef acDoc As Document, ByRef acObj As Object) As ObjectId
    Dim objId As ObjectId
    Dim acCurDb As Database = acDoc.Database 'Get the current database
    Dim acBlkTbl As BlockTable
    Dim acBlkTblRec As BlockTableRecord

    Using lock As DocumentLock = acDoc.LockDocument
        'Start a transaction
        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            'Open Model space for write
            acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
            acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            acObj.SetDatabaseDefaults()

            'Add the object to the drawing
            objId = acBlkTblRec.AppendEntity(acObj)
            acTrans.AddNewlyCreatedDBObject(acObj, True)

            'Commit the changes and dispose of the transaction
            acTrans.Commit()
        End Using
    End Using

    Return objId
End Function

我不太确定如何将修剪应用到线上。我已经看过一些IntersectWith方法,但还无法使用它。我会同时处理这个问题,如果我找到答案,我一定会在这里发布。

1 个答案:

答案 0 :(得分:3)

你可以利用一些处理曲线的很酷的方法来处理这个问题:

Private Shared Function TrimmedLine(line As Line, ent As Entity) As Line
    If line Is Nothing Then
        Throw New ArgumentNullException("line")
    End If

    ' Original line is returned since there's nothing to break it
    If ent Is Nothing Then Return line

    Dim extPoints = New Point3dCollection()
    Try
        line.IntersectWith(ent, Intersect.ExtendArgument, extPoints, IntPtr.Zero, IntPtr.Zero)

        ' Original line gets returned since it doesn't intersect
        If extPoints.Count = 0 Then Return line

        Dim splitLines = line.GetSplitCurves(extPoints)

        ' Not sure when this would fail, investigate.
        If splitLines.Count = 0 Then Return Nothing

        ' Return the outer line in this case
        Return DirectCast(splitLines(0), Line)

    Catch ex As Autodesk.AutoCAD.Runtime.Exception
        System.Diagnostics.Debug.Write(ex.Message)
        Throw
    End Try
End Function