Imports System.IO
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.GraphicsInterface
Imports Autodesk.AutoCAD.Runtime
Public Class MoveRotateScaleJig
Inherits DrawJig
Public Shared entities As List(Of Entity) = New List(Of Entity)()
Private [step] As Integer = 1
Private totalStepNum As Integer = 3
Public moveStartPnt As Point3d
Public Shared moveEndPnt As Point3d
Public rotateAngle As Double
Public scaleFactor As Double
Public Sub New(ByVal basePnt As Point3d)
moveStartPnt = basePnt
moveEndPnt = moveStartPnt
rotateAngle = 0
scaleFactor = 1
End Sub
Public ReadOnly Property Transformation As Matrix3d
Get
Return Matrix3d.Scaling(scaleFactor, moveEndPnt).PostMultiplyBy(Matrix3d.Rotation(rotateAngle, Vector3d.ZAxis, moveEndPnt)).PostMultiplyBy(Matrix3d.Displacement(moveStartPnt.GetVectorTo(moveEndPnt)))
End Get
End Property
Public Sub AddEntity(ByVal ent As Entity)
entities.Add(ent)
End Sub
Public Sub TransformEntities()
Dim mat As Matrix3d = Transformation
For Each ent As Entity In entities
ent.TransformBy(mat)
Next
End Sub
Protected Overrides Function WorldDraw(ByVal draw As Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean
Dim mat As Matrix3d = Transformation
Dim geo As WorldGeometry = draw.Geometry
If geo IsNot Nothing Then
geo.PushModelTransform(mat)
For Each ent As Entity In entities
geo.Draw(ent)
Next
geo.PopModelTransform()
End If
Return True
End Function
Protected Overrides Function Sampler(ByVal prompts As JigPrompts) As SamplerStatus
Select Case [step]
Case 1
Dim prOptions1 As JigPromptPointOptions = New JigPromptPointOptions(vbLf & "目标点:")
prOptions1.UserInputControls = UserInputControls.GovernedByOrthoMode Or UserInputControls.GovernedByUCSDetect
Dim prResult1 As PromptPointResult = prompts.AcquirePoint(prOptions1)
If prResult1.Status <> PromptStatus.OK Then Return SamplerStatus.Cancel
If prResult1.Value.Equals(moveEndPnt) Then
Return SamplerStatus.NoChange
Else
moveEndPnt = prResult1.Value
Return SamplerStatus.OK
End If
Case Else
End Select
Return SamplerStatus.OK
End Function
Public Shared Function Jig() As Boolean
Try
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim selRes As PromptSelectionResult = doc.Editor.GetSelection()
If selRes.Status <> PromptStatus.OK Then Return False
Dim ppr As PromptPointResult = doc.Editor.GetPoint(vbLf & "Start point:")
If ppr.Status <> PromptStatus.OK Then Return False
Dim basePnt As Point3d = ppr.Value
basePnt = basePnt.TransformBy(doc.Editor.CurrentUserCoordinateSystem)
Dim jig_ As MoveRotateScaleJig = New MoveRotateScaleJig(basePnt)
Using tr As Transaction = db.TransactionManager.StartTransaction()
For Each id As ObjectId In selRes.Value.GetObjectIds()
Dim ent As Entity = CType(tr.GetObject(id, OpenMode.ForWrite), Entity)
jig_.AddEntity(ent)
Next
Dim pr As PromptResult
Do
pr = doc.Editor.Drag(jig_)
If pr.Status = PromptStatus.Keyword Then
Else
jig_.[step] += 1
End If
Loop While pr.Status = PromptStatus.OK AndAlso jig_.[step] <= jig_.totalStepNum
If pr.Status = PromptStatus.OK AndAlso jig_.[step] = jig_.totalStepNum + 1 Then
jig_.TransformEntities()
Else
Return False
End If
tr.Commit()
Return True
End Using
Catch
Return False
End Try
End Function
End Class
Public Class MoveJig
<CommandMethod("MoveJig")>
Public Shared Sub MoveJig()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
If MoveRotateScaleJig.Jig() Then
doc.Editor.WriteMessage(vbLf & "success" & vbLf)
Else
doc.Editor.WriteMessage(vbLf & "failure" & vbLf)
End If
End Sub
End Class
vbnet CAD 模拟移动。
猜你喜欢
转载自blog.csdn.net/laocooon/article/details/119884047
今日推荐
周排行