Private Sub InputButton1_Click(sender As Object, e As EventArgs) Handles InputButton1.Click
Me.CancelCommands()
using Active.Document.LockDocument()
Using transaction = Active.Database.TransactionManager.StartTransaction()
Dim modelSpace = Active.Database.ModelSpace(OpenMode.ForWrite)
Dim uBolt = CreateUBolt(0.375, 3.5, 3.0)
If uBolt IsNot Nothing Then
modelSpace.AppendEntity(uBolt)
transaction.AddNewlyCreatedDBObject(uBolt, True)
Else
Active.WriteMessage("Unable to create a UBolt with the current dimensions.")
End If
transaction.Commit()
End Using
End Using
End Sub
''' <summary>
''' Cancels any current command by sending escape to the command line twice.
''' </summary>
Private Sub CancelCommands()
If CShort(CoreApplication.GetSystemVariable("CMDACTIVE")) <> 0 Then
Active.Document.SendStringToExecute(ChrW(27) & ChrW(27), False, True, False)
Utils.PostCommandPrompt()
End If
End Sub
Private Const MinimumExtrusionLength = .001
''' <summary>
''' Createsa 3D UBolt with a center point at the world origin. To change the centerpoint, tranform the solid after creation.
''' </summary>
''' <param name="uBoltDiameter">The u bolt diameter.</param>
''' <param name="pipeDiameter">The pipe diameter.</param>
''' <param name="extendedLength">The extended length of the ubolt beyond the diameter..</param>
''' <returns>A Solid3D representation of the UBolt</returns>
''' <exception cref="System.ArgumentOutOfRangeException">
''' UBolt Diameter is too small.
''' or
''' Pipe Diameter is to small.
''' </exception>
Private Function CreateUBolt(uBoltDiameter As Double, pipeDiameter As Double, extendedLength As Double) As Solid3d
If uBoltDiameter <= MinimumExtrusionLength Then
Throw New ArgumentOutOfRangeException(NameOf(uBoltDiameter), "UBolt Diameter is too small.")
End If
If pipeDiameter <= MinimumExtrusionLength Then
Throw New ArgumentOutOfRangeException(NameOf(pipeDiameter), "Pipe Diameter is to small.")
End If
Dim centerToCenter = (pipeDiameter / 2) + uBoltDiameter
' While not probable, it is possible that the sum value is greater then the largest double value. Input values for
' extended length should take this into account.
Dim straightLength = (pipeDiameter / 2) + extendedLength
Dim uBolt = New Solid3d()
Dim polylineSegmentCollection = New PolylineSegmentCollection()
using lineSegment1 = New LineSegment2d(New Point2d(-1 * centerToCenter, straightLength), New Point2d(-1 * centerToCenter, straightLength - minimumExtrusionLength))
Dim lineSegment = New PolylineSegment(lineSegment1)
polylineSegmentCollection.Add(lineSegment)
End Using
Using lineSegment2 = New LineSegment2d(New Point2d(-1 * centerToCenter, straightLength - minimumExtrusionLength), New Point2d(-1 * centerToCenter, 0))
Dim lineSegment = new PolylineSegment(lineSegment2)
polylineSegmentCollection.Add(lineSegment)
End Using
Dim arcSegment = new PolylineSegment(New Point2d(-1 * centerToCenter, 0), New Point2d(centerToCenter, 0), 1, 0)
polylineSegmentCollection.Add(arcSegment)
Using lineSegment3 = New LineSegment2d(New Point2d(centerToCenter, 0), New Point2d(centerToCenter, straightLength))
Dim lineSegment = new PolylineSegment(lineSegment3)
polylineSegmentCollection.Add(lineSegment)
End Using
Using centerLine = polylineSegmentCollection.ToPolyline()
Using profile = New Circle(centerLine.StartPoint, Vector3d.YAxis, uBoltDiameter)
Try
Dim profileCollection = New DBObjectCollection()
profileCollection.Add(profile)
Dim profileRegionCollection = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(profileCollection)
Dim profileRegion As Region = profileRegionCollection(0)
uBolt.ExtrudeAlongPath(profileRegion, centerLine, 0.0)
uBolt.CleanBody()
Catch exception As Exception
Log.Logger.Error(exception, "Unable to extrude the 3D Solid")
Return Nothing
End Try
End Using
End Using
Return uBolt
End Function