Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Namespace SolidCreation
Public Class Commands
<CommandMethod("SAP")> _
Public Sub SweepAlongPath()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
' Ask the user to select a region to extrude
Dim peo1 As New PromptEntityOptions(vbLf & "Select profile or curve to sweep: ")
peo1.SetRejectMessage(vbLf & "Entity must be a region, curve or planar surface.")
peo1.AddAllowedClass(GetType(Region), False)
peo1.AddAllowedClass(GetType(Curve), False)
peo1.AddAllowedClass(GetType(PlaneSurface), False)
Dim per As PromptEntityResult = ed.GetEntity(peo1)
If per.Status <> PromptStatus.OK Then
Return
End If
Dim regId As ObjectId = per.ObjectId
' Ask the user to select an extrusion path
Dim peo2 As New PromptEntityOptions(vbLf & "Select path along which to sweep: ")
peo2.SetRejectMessage(vbLf & "Entity must be a curve.")
peo2.AddAllowedClass(GetType(Curve), False)
per = ed.GetEntity(peo2)
If per.Status <> PromptStatus.OK Then
Return
End If
Dim splId As ObjectId = per.ObjectId
Dim pko As New PromptKeywordOptions(vbLf & "Sweep a solid or a surface?")
pko.AllowNone = True
pko.Keywords.Add("SOlid")
pko.Keywords.Add("SUrface")
pko.Keywords.[Default] = "SOlid"
Dim pkr As PromptResult = ed.GetKeywords(pko)
Dim createSolid As Boolean = (pkr.StringResult = "SOlid")
If pkr.Status <> PromptStatus.OK Then
Return
End If
' Now let's create our swept surface
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Using tr
Try
Dim sweepEnt As Entity = TryCast(tr.GetObject(regId, OpenMode.ForRead), Entity)
Dim pathEnt As Curve = TryCast(tr.GetObject(splId, OpenMode.ForRead), Curve)
If sweepEnt Is Nothing OrElse pathEnt Is Nothing Then
ed.WriteMessage(vbLf & "Problem opening the selected entities.")
Return
End If
' We use a builder object to create
' our SweepOptions
Dim sob As New SweepOptionsBuilder()
' Align the entity to sweep to the path
sob.Align = SweepOptionsAlignOption.AlignSweepEntityToPath
' The base point is the start of the path
sob.BasePoint = pathEnt.StartPoint
' The profile will rotate to follow the path
sob.Bank = True
' Now generate the solid or surface...
Dim ent As Entity
If createSolid Then
Dim sol As New Solid3d()
sol.CreateSweptSolid(sweepEnt, pathEnt, sob.ToSweepOptions())
ent = sol
Else
Dim ss As New SweptSurface()
ss.CreateSweptSurface(sweepEnt, pathEnt, sob.ToSweepOptions())
ent = ss
End If
' ... and add it to the modelspace
Dim bt As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
Dim ms As BlockTableRecord = CType(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
ms.AppendEntity(ent)
tr.AddNewlyCreatedDBObject(ent, True)
tr.Commit()
Catch
End Try
End Using
End Sub
End Class
End Namespace
http://through-the-interface.typepad.com/through_the_interface/2010/01/sweeping-an-autocad-solid ... 1348761e46f970c#comment-6a00d83452464869e201348761e46f970c
[本日志由 tiancao1001 于 2017-07-31 03:31 PM 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |