Author Topic: Chaining multiple jigs together in c# .NET  (Read 2474 times)

0 Members and 1 Guest are viewing this topic.

AlexFielder

  • Mosquito
  • Posts: 11
Chaining multiple jigs together in c# .NET
« on: October 18, 2023, 04:52:14 AM »
Hi folks,

After spending (probably far too long) revisiting the old jig code I mentioned in my previous post, I am back thinking about how I might achieve a dynamic block insertion with a rotation and a subsequent linear array of said block.

The following code shared by gile__ some ten years ago shows very simply how to jig the initial placement, and then add a rotation and I wonder if it is possible to also add a drawjig that would allow me to array the placed block along the vector of the intial rotated placement?

Code: [Select]
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime

<Assembly: CommandClass(GetType(InsertRotateJigSample.CommandMethods))>

Namespace InsertRotateJigSample

    Public Class CommandMethods

        <CommandMethod("Test", CommandFlags.Modal)> _
        Public Sub Test()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor

            Dim pr As PromptResult = ed.GetString(vbLf & "Enter the block name: ")
            If pr.Status <> PromptStatus.OK Then
                Return
            End If
            Dim blkName As String = pr.StringResult

            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim bt As BlockTable = _
                    DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                If Not bt.Has(blkName) Then
                    ed.WriteMessage(vbLf & "Block '{0}' not found.", blkName)
                    Return
                End If
                Using br As New BlockReference(Point3d.Origin, bt(blkName))
                    br.TransformBy(ed.CurrentUserCoordinateSystem)

                    ' Using InsertBlockJig class to insert the block
                    Dim insertJig As New InsertBlockJig(br)
                    pr = ed.Drag(insertJig)
                    If pr.Status <> PromptStatus.OK Then
                        Return
                    End If

                    ' Using RotateBlockJig class to rotate the block
                    Dim rotateJig As New RotateBlockJig(br)
                    pr = ed.Drag(rotateJig)
                    If pr.Status <> PromptStatus.OK Then
                        Return
                    End If
                    rotateJig.UpdateRotation()

                    Dim btr As BlockTableRecord = _
                        DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                    btr.AppendEntity(br)
                    tr.AddNewlyCreatedDBObject(br, True)
                End Using
                tr.Commit()
            End Using
        End Sub

    End Class

    Class InsertBlockJig
        Inherits EntityJig

        ' Protected fields
        Protected position As Point3d
        Protected br As BlockReference

        ' Constructor (fields initialization)
        Public Sub New(br As BlockReference)
            MyBase.New(br)
            Me.br = br
            Me.position = br.Position
        End Sub

        ' Prompts the user to specify the insertion point (EntityJig implementation)
        Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus
            Dim msg As String = vbLf & "Specify the insertion point: "
            Dim jppo As New JigPromptPointOptions(msg)
            jppo.UserInputControls = (UserInputControls.Accept3dCoordinates Or _
                                      UserInputControls.NullResponseAccepted)
            Dim ppr As PromptPointResult = prompts.AcquirePoint(jppo)
            If Me.position.DistanceTo(ppr.Value) < Tolerance.[Global].EqualPoint Then
                Return SamplerStatus.NoChange
            Else
                Me.position = ppr.Value
            End If
            Return SamplerStatus.OK
        End Function

        ' Updates the bloc position (EntityJig implementation)
        Protected Overrides Function Update() As Boolean
            Me.br.Position = Me.position
            Return True
        End Function
    End Class

    Class RotateBlockJig
        Inherits EntityJig

        ' Private fields
        Protected br As BlockReference
        Protected rot As Double, ucsRot As Double

        ' Constructor
        Public Sub New(br As BlockReference)
            MyBase.New(br)
            Me.br = br
            Me.ucsRot = br.Rotation
        End Sub

        ' Prompts the user to specify the rotation (EntityJig implementation)
        Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus
            Dim jpao As New JigPromptAngleOptions(vbLf & "Specify the rotation: ")
            jpao.DefaultValue = 0.0
            jpao.UseBasePoint = True
            jpao.BasePoint = Me.br.Position
            jpao.Cursor = CursorType.RubberBand
            jpao.UserInputControls = (UserInputControls.Accept3dCoordinates Or _
                                      UserInputControls.UseBasePointElevation Or _
                                      UserInputControls.NullResponseAccepted)
            Dim pdr As PromptDoubleResult = prompts.AcquireAngle(jpao)

            If Me.rot = pdr.Value Then
                Return SamplerStatus.NoChange
            Else
                Me.rot = pdr.Value
                Return SamplerStatus.OK
            End If
        End Function

        ' Updates the bloc rotation (EntityJig implementation)
        Protected Overrides Function Update() As Boolean
            UpdateRotation()
            Return True
        End Function

        ' Updates the bloc rotation (mandatory for the 'default' option)
        ' This method is called from the method where the jig is created
        Friend Sub UpdateRotation()
            Me.br.Rotation = Me.rot + Me.ucsRot
        End Sub
    End Class

End Namespace

Any pointers gratefully received.

Thanks,

Alex.