AutoCAD .NET API provides two concrete Jig classes for us to jig different entities in different circumstances, EntityJig and DrawJig. EntityJig is to jig a specific entity as its name indicates and the DrawJig is to jig anything that has graphics to draw, which can be a single entity, a group of entities, or something that is not available natively in AutoCAD.
In this article, let us see how to move a Block (Commonly used name)/BlockReference (API term) /INSERT (User term) using AutoCAD EntityJig and VB.NET. Here is the core code and a test command for the block moving jig we are going to talk about:
#Region "Namespaces"
Imports System.Text
Imports System.Linq
Imports System.Xml
Imports System.Reflection
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Windows.Forms
Imports System.Drawing
Imports System.IO
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Windows
Imports MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application
Imports MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document
Imports AcWindowsNS = Autodesk.AutoCAD.Windows
#End Region
Namespace AcadNetAddinWizard_Namespace
Public Class BlockMoving
Inherits EntityJig
#Region "Fields"
Public mCurJigFactorNumber As Integer = 1
Private mPosition As New Point3d()
' Factor #1
#End Region
#Region "Constructors"
Public Sub New(ent As Entity)
MyBase.New(ent)
End Sub
#End Region
#Region "Overrides"
Protected Overrides Function Update() As Boolean
Select Case mCurJigFactorNumber
Case 1
TryCast(Entity, BlockReference).Position = mPosition
Exit Select
Case Else
Return False
End Select
Return True
End Function
Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus
Select Case mCurJigFactorNumber
Case 1
Dim prOptions1 As New JigPromptPointOptions(vbLf & "Block position:")
Dim prResult1 As PromptPointResult = prompts.AcquirePoint(prOptions1)
If prResult1.Status = PromptStatus.Cancel Then
Return SamplerStatus.Cancel
End If
If prResult1.Value.Equals(mPosition) Then
Return SamplerStatus.NoChange
Else
mPosition = prResult1.Value
Return SamplerStatus.OK
End If
Case Else
Exit Select
End Select
Return SamplerStatus.OK
End Function
#End Region
#Region "Method to Call"
Public Shared Function Jig(ent As BlockReference) As Boolean
Try
Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
Dim jigger As New BlockMoving(ent)
Dim pr As PromptResult
Do
pr = ed.Drag(jigger)
jigger.mCurJigFactorNumber += 1
Loop While pr.Status <> PromptStatus.Cancel AndAlso pr.Status <> PromptStatus.[Error] AndAlso pr.Status <> PromptStatus.Keyword AndAlso jigger.mCurJigFactorNumber <= 1
Return pr.Status = PromptStatus.OK
Catch
Return False
End Try
End Function
#End Region
End Class
End Namespace
…
<CommandMethod("TestEntityJigger4")> _
Public Shared Sub TestEntityJigger4_Method()
Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
Dim db As Database = HostApplicationServices.WorkingDatabase
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim selRes As PromptEntityResult = ed.GetEntity("Pick a block:")
If selRes.Status = PromptStatus.OK Then
Dim ent As BlockReference = TryCast(tr.GetObject(selRes.ObjectId, OpenMode.ForWrite), BlockReference)
If ent IsNot Nothing Then
BlockMoving.Jig(ent)
End If
End If
tr.Commit()
End Using
End Sub
…
Here is how the block moving jig behaves in AutoCAD after the test command runs and a block is picked:
As can be seen, the original block stayed at its original place for the reference purpose in this situation and some exact graphics as the block appearance were moving along with the cursor at that moment. After a point (the new block position) was clicked, the picked block was moved to the new place.
If readers would like a different behavior for the block moving such as the original block graphics being highlighted or even making it disappear totally, it can be achieved by tweaking the test command a bit. Some similar code will be introduced in some coming posts, please stay tuned.
A few highlights about the code may be helpful:
• An entity type needs to be specified in the EntityJig derivative, as BlockReference here.
• The Sampler() override is to acquire input for the new block position.
• If the input is the same as the stored variable, we’d better return SamplerStatus.NoChange to avoid unnecessary flashing; if not, return SamplerStatus.OK.
• Please do not forget to handle the cancel/escape circumstance as demonstrated.
• The Update() override is to update the block position as set in the Sampler().
• The Editor.Draw() is the power to fire the jig.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a coder, Entity Jigger, to help us create entity jig code automatically, quickly and reliably to address various entity types and circumstances.
Recent Comments