Question

Goal

To trim a line in an ellipse that goes to it's center. The portion of the line that enters the ellipse should be trimmed off.

This is the untrimmed ellipse

Untrimmed

This is the trimmed ellipse, the goal of this question

Trimmed


Attempt

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

I'm not quite sure how to apply the trim to the line. I've seen some IntersectWith methods but couldn't get it to work yet. I'll be working on this meanwhile and if I find the answer I'll be sure to post it here.

Was it helpful?

Solution

You can take advantage of a few cool methods dealing with curves to handle this:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top