[go: up one dir, main page]

0% found this document useful (0 votes)
408 views16 pages

VB.NET Jig Creation Guide for AutoCAD

This document provides instructions for a hands-on lab to create jigs using VB.NET in AutoCAD. It discusses the basics of jigs and how to use a DrawJig. It then walks through 5 steps of code for a DrawJig class, making incremental changes to the code at each step to demonstrate how to draw different geometry, change line colors, and conditionally display elements based on the jig state. The goal is for students to learn the fundamentals of jig creation and go beyond the basics by modifying the example code.

Uploaded by

G. Ali Blackburn
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
408 views16 pages

VB.NET Jig Creation Guide for AutoCAD

This document provides instructions for a hands-on lab to create jigs using VB.NET in AutoCAD. It discusses the basics of jigs and how to use a DrawJig. It then walks through 5 steps of code for a DrawJig class, making incremental changes to the code at each step to demonstrate how to draw different geometry, change line colors, and conditionally display elements based on the jig state. The goal is for students to learn the fundamentals of jig creation and go beyond the basics by modifying the example code.

Uploaded by

G. Ali Blackburn
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Hands-On Jigging Creating Jigs using VB.

NET
Jerry Winters VB CAD, Inc.

CP122-2L It is one thing to see someone else create a jig. It's quite another to create one yourself. You can do just that in this hands-on lab. Actually, you will create several jigs in this class. Learn the basics and how to go beyond the basics of jig creation using VB.NET.

About the Speaker:


Jerry has emerged as one of the top-rated speakers at Autodesk University on programming topics (VBA, VB.NET, and C# in particular). Forget about the great "stress reduction techniques" that result in AU attendees leaving with everything from power tools, climbing ropes, and remote control carsthere's more to it than this. Jerry's sense of humor and his "I'm just a drafter" approach to programming results in students coming to an understanding of the topic and leaving with a few chuckles. jerryw@vbcad.com

Hands-On Jigging Creating Jigs using VB.NET

Hands-On Jigging Creating Jigs using VB.NET

Jig Basics
A Jig is a tool whereby graphics are displayed as the user is prompted to select a point, angle, etc. Think of the Copy command. When objects are selected and a base point has been selected, the Jig jumps in and displays the objects as the cursor is moved on the screen. When a Block is inserted, a Jig is used to display the block as the cursor moves around the screen. You issue the Move command. You see the objects moving as you move your cursor. Its a jig. Jigs dont stop there, though. A Jig is meant to aid in the users design work in AutoCAD.

Using a DrawJig
We are going to begin by working with a DrawJig. Open the solution in the Documents\Visual Studio 2008\Projects\AU2009 DrawJigA Folder. It is named DrawJigA.sln.

Hands-On Jigging Creating Jigs using VB.NET

Lets try running this Jig by hitting the F5 key on the keyboard. AutoCAD should start automatically. Next, were going to use the netload command and select the .dll file that was created when we began the debugging process (debugging starts by pressing the F5 key).

Select DrawJigA and click Open. When we complete the Netload command, we can issue the command defined in our project. In this case, the name of the new command is DoJigA. This Jig draws a circle at the cursor location and a line from (0, 0, 0) to the cursor location. We will see all of the Jig code on the next page. Please note that this code is in a Class named AU_DrawJig.

Hands-On Jigging Creating Jigs using VB.NET

STEP 1

Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Geometry Public Class AU_DrawJig Inherits Autodesk.AutoCAD.EditorInput.DrawJig Dim BasePt As Point3d Dim myPR As PromptPointResult Function StartJig() As PromptPointResult Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor myPR = ed.Drag(Me) Do Select Case myPR.Status Case PromptStatus.OK Return myPR Exit Do End Select Loop While myPR.Status <> PromptStatus.Cancel Return myPR End Function Protected Overrides Function Sampler(ByVal prompts As _ Autodesk.AutoCAD.EditorInput.JigPrompts) As _ Autodesk.AutoCAD.EditorInput.SamplerStatus myPR = prompts.AcquirePoint("Select a point:") If myPR.Value.IsEqualTo(BasePt) Then Return SamplerStatus.NoChange Else BasePt = myPR.Value Return SamplerStatus.OK End If End Function Protected Overrides Function WorldDraw(ByVal draw As _ Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean draw.Geometry.WorldLine(New Point3d(0, 0, 0), BasePt) draw.Geometry.Circle(BasePt, 1.5, Vector3d.ZAxis) ' draw.Geometry.Draw(New Line(New Point3d(0, 0, 0), BasePt)) ' draw.Geometry.Draw(New Circle(BasePt, Vector3d.ZAxis, 1.5)) End Function End Class

Hands-On Jigging Creating Jigs using VB.NET

Lets start by looking at the WorldDraw Function (at the bottom of the previous page). This is where we specify what is to be drawn. The BasePt variable is a Point3d object. Lets review the code:
draw.Geometry.WorldLine(New Point3d(0, 0, 0), BasePt) draw.Geometry.Circle(BasePt, 1.5, Vector3d.ZAxis)

We are drawing a Line from (0, 0, 0) to the BasePt. The BasePt is where the cursor is located. Then we draw a Circle centered at the BasePt with a radius of 1.5 with a ZAxis normal. Lets try commenting out the above two lines of code (by using the apostrophe punctuation in front of the line of code) and removing the comment in the two lines of code below. Your code should look like this:
Protected Overrides Function WorldDraw(ByVal draw As _ Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean ' draw.Geometry.WorldLine(New Point3d(0, 0, 0), BasePt) ' draw.Geometry.Circle(BasePt, 1.5, Vector3d.ZAxis) draw.Geometry.Draw(New Line(New Point3d(0, 0, 0), BasePt)) draw.Geometry.Draw(New Circle(BasePt, Vector3d.ZAxis, 1.5)) End Function

When we begin debugging again and load our project using Netload, we can run the DoJigA command again. The jig should look just like it did previously even though we are drawing the Line and Circle using a different API call. The next couple of pages are going to show some changes to our DrawJig. The lines of code that change are shown in Bold.

Hands-On Jigging Creating Jigs using VB.NET

STEP 2

Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Geometry Public Class AU_DrawJig Inherits Autodesk.AutoCAD.EditorInput.DrawJig Dim BasePt As Point3d Dim myPR As PromptPointResult Dim myLine As New Line() Function StartJig() As PromptPointResult myLine.StartPoint = New Point3d(0, 0, 0) Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor myPR = ed.Drag(Me) Do Select Case myPR.Status Case PromptStatus.OK Return myPR Exit Do End Select Loop While myPR.Status <> PromptStatus.Cancel Return myPR End Function Protected Overrides Function Sampler(ByVal prompts As _ Autodesk.AutoCAD.EditorInput.JigPrompts) As _ Autodesk.AutoCAD.EditorInput.SamplerStatus myPR = prompts.AcquirePoint("Select a point:") If myPR.Value.IsEqualTo(BasePt) Then Return SamplerStatus.NoChange Else BasePt = myPR.Value myLine.EndPoint = BasePt Return SamplerStatus.OK End If End Function Protected Overrides Function WorldDraw(ByVal draw As _ Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean ' draw.Geometry.WorldLine(New Point3d(0, 0, 0), BasePt) ' draw.Geometry.Circle(BasePt, 1.5, Vector3d.ZAxis) draw.Geometry.Draw(myLine) draw.Geometry.Draw(New Circle(BasePt, Vector3d.ZAxis, 1.5)) End Function Protected Overrides Sub Finalize() myLine.Dispose() MyBase.Finalize() End Sub End Class

Hands-On Jigging Creating Jigs using VB.NET

STEP 3
Imports Imports Imports Imports

Autodesk.AutoCAD.ApplicationServices Autodesk.AutoCAD.DatabaseServices Autodesk.AutoCAD.EditorInput Autodesk.AutoCAD.Geometry

Public Class AU_DrawJig Inherits Autodesk.AutoCAD.EditorInput.DrawJig Dim BasePt As Point3d Dim myPR As PromptPointResult Dim myLine As New Line() Function StartJig() As PromptPointResult myLine.StartPoint = New Point3d(0, 0, 0) myLine.ColorIndex = 1 Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor myPR = ed.Drag(Me) Do Select Case myPR.Status Case PromptStatus.OK Return myPR Exit Do End Select Loop While myPR.Status <> PromptStatus.Cancel Return myPR End Function Protected Overrides Function Sampler(ByVal prompts As _ Autodesk.AutoCAD.EditorInput.JigPrompts) As _ Autodesk.AutoCAD.EditorInput.SamplerStatus myPR = prompts.AcquirePoint("Select a point:") If myPR.Value.IsEqualTo(BasePt) Then Return SamplerStatus.NoChange Else BasePt = myPR.Value myLine.EndPoint = BasePt Return SamplerStatus.OK End If End Function Protected Overrides Function WorldDraw(ByVal draw As _ Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean ' draw.Geometry.WorldLine(New Point3d(0, 0, 0), BasePt) ' draw.Geometry.Circle(BasePt, 1.5, Vector3d.ZAxis) draw.Geometry.Draw(myLine) draw.Geometry.Draw(New Circle(BasePt, Vector3d.ZAxis, 1.5)) End Function Protected Overrides Sub Finalize() myLine.Dispose() MyBase.Finalize() End Sub End Class

Hands-On Jigging Creating Jigs using VB.NET

Hands-On Jigging Creating Jigs using VB.NET

The line is now red as the Jig is displayed. Lets see what else we can do with the line color. The example below bases the color of the line on the length of the line. If the length is greater than 10, the line is red. If not, the line is Black.

Function StartJig() As PromptPointResult myLine.StartPoint = New Point3d(0, 0, 0) myLine.ColorIndex = 1 Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor myPR = ed.Drag(Me) Do Select Case myPR.Status Case PromptStatus.OK Return myPR Exit Do End Select Loop While myPR.Status <> PromptStatus.Cancel Return myPR End Function Protected Overrides Function Sampler(ByVal prompts As _ Autodesk.AutoCAD.EditorInput.JigPrompts) As _ Autodesk.AutoCAD.EditorInput.SamplerStatus myPR = prompts.AcquirePoint("Select a point:") If myPR.Value.IsEqualTo(BasePt) Then Return SamplerStatus.NoChange Else BasePt = myPR.Value If myLine.Length > 10 Then myLine.ColorIndex = 1 Else myLine.ColorIndex = 0 End If myLine.EndPoint = BasePt Return SamplerStatus.OK End If End Function

STEP 4

10

Hands-On Jigging Creating Jigs using VB.NET

Heres the next change:

Protected Overrides Function WorldDraw(ByVal draw As _ Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean ' draw.Geometry.WorldLine(New Point3d(0, 0, 0), BasePt) ' draw.Geometry.Circle(BasePt, 1.5, Vector3d.ZAxis) draw.Geometry.Draw(myLine) If myLine.Length <= 10 Then draw.Geometry.Draw(New Circle(BasePt, Vector3d.ZAxis, 1.5)) End If End Function

STEP 5

Dont run it yet. Take a look at the code. Whats going to happen? If the length of the line is less than or equal to 10, the Circle is drawn. Otherwise, the only thing we will see is a red line. Next change:

Protected Overrides Function WorldDraw(ByVal draw As _ Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean ' draw.Geometry.WorldLine(New Point3d(0, 0, 0), BasePt) ' draw.Geometry.Circle(BasePt, 1.5, Vector3d.ZAxis) draw.Geometry.Draw(myLine) If myLine.Length <= 10 Then draw.Geometry.Draw(New Circle(BasePt, Vector3d.ZAxis, 1.5)) Else Dim tmpline As Line = myLine.Clone Dim myMat As New Matrix3d(Matrix3d.Rotation(5 * Math.PI / 180, _ Vector3d.ZAxis, New Point3d(0, 0, 0)).ToArray) tmpline.TransformBy(myMat) draw.Geometry.Draw(tmpline) myMat = New Matrix3d(Matrix3d.Rotation(-10 * Math.PI / 180, _ Vector3d.ZAxis, New Point3d(0, 0, 0)).ToArray) tmpline.TransformBy(myMat) draw.Geometry.Draw(tmpline) End If End Function

STEP 6

What does this do? Now, when the line turns red, it is also cloned and rotated 5 degrees on each side of the cursor to show three lines.

11

Hands-On Jigging Creating Jigs using VB.NET

Creating an Entity Jig


A DrawJig is used to Jig one or more entities. An Entity Jig only draws one entity. This is the type of Jig you would use for Jigging a Block Reference. Lets load the Solution under Documents\Visual Studio 2008\Projects\AU2009 InsertBlockJig. Heres the code in the Class.vb file:

Imports Imports Imports Imports Imports

Autodesk.AutoCAD Autodesk.AutoCAD.Runtime Autodesk.AutoCAD.EditorInput Autodesk.AutoCAD.DatabaseServices Autodesk.AutoCAD.ApplicationServices

Public Class AU_2009Class <CommandMethod("InsertBlockJig")> _ Public Sub InsertBlockJig() InsertBlockWithJig("Door") End Sub Public Sub InsertBlockWithJig(ByVal BlockName As String) Dim myDB As Database myDB = HostApplicationServices.WorkingDatabase Dim myJig As AU_BlockJig Using myTrans As Transaction = myDB.TransactionManager.StartTransaction Dim myBT As BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead) If myBT.Has(BlockName) Then Dim myBTR As BlockTableRecord = _ myBT(BlockName).GetObject(OpenMode.ForRead) myJig = New AU_BlockJig(New Geometry.Point3d(0, 0, 0), _ myBTR.ObjectId) Else Exit Sub End If End Using Dim myBlkID As ObjectId Dim SelPt As EditorInput.PromptPointResult Do SelPt = myJig.BeginJig() If Not SelPt Is Nothing Then Select Case SelPt.Status Case EditorInput.PromptStatus.OK myBlkID = InsertBlock(SelPt.Value, _ BlockName, 1, 1, 1) Case EditorInput.PromptStatus.Other Exit Sub End Select End If If SelPt Is Nothing Then Exit Do Loop While SelPt.Status = EditorInput.PromptStatus.OK End Sub Public Function InsertBlock(ByVal InsPt As Geometry.Point3d, _ ByVal BlockName As String, ByVal XScale As Double, _ ByVal YScale As Double, ByVal ZScale As Double) _

12

Hands-On Jigging Creating Jigs using VB.NET

As DatabaseServices.ObjectId Dim myBlockRef As BlockReference Dim myDB As Database = HostApplicationServices.WorkingDatabase Using myTrans As Transaction = myDB.TransactionManager.StartTransaction Dim myBT As BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead) Dim myBTR As BlockTableRecord = _ myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite) 'Insert the Block Dim myBlockDef As BlockTableRecord = _ myBT(BlockName).GetObject(OpenMode.ForRead) myBlockRef = New DatabaseServices.BlockReference(InsPt, myBT(BlockName)) myBlockRef.ScaleFactors = New Geometry.Scale3d(XScale, YScale, ZScale) myBTR.AppendEntity(myBlockRef) myTrans.AddNewlyCreatedDBObject(myBlockRef, True) 'Set the Attribute Value Dim myAttColl As DatabaseServices.AttributeCollection Dim myEnt As DatabaseServices.Entity myAttColl = myBlockRef.AttributeCollection For Each entID As ObjectId In myBlockDef myEnt = entID.GetObject(OpenMode.ForWrite) If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then Dim myAttDef As DatabaseServices.AttributeDefinition = myEnt Dim myAttRef As New DatabaseServices.AttributeReference myAttRef.SetAttributeFromBlock(myAttDef, _ myBlockRef.BlockTransform) myAttColl.AppendAttribute(myAttRef) myTrans.AddNewlyCreatedDBObject(myAttRef, True) End If Next myTrans.Commit() End Using Return myBlockRef.ObjectId End Function End Class

And Now for the Jig Class:

Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD Public Class AU_BlockJig Inherits Autodesk.AutoCAD.EditorInput.EntityJig Dim BasePt As Point3d = New Point3d(0, 0, 0) Dim myMatrix As Matrix3d Dim myBRef As DatabaseServices.BlockReference Dim myOpts As EditorInput.JigPromptPointOptions Sub New(ByVal BlockIns As Point3d, ByVal BlockID As ObjectId) MyBase.New(New DatabaseServices.BlockReference(BlockIns, BlockID)) myBRef = Me.Entity End Sub Function BeginJig() As PromptPointResult

13

Hands-On Jigging Creating Jigs using VB.NET

If myOpts Is Nothing Then myOpts = New EditorInput.JigPromptPointOptions() myOpts.Message = vbCrLf & "Select a point:" myOpts.Cursor = EditorInput.CursorType.Invisible myOpts.UseBasePoint = False End If Dim ed As EditorInput.Editor = _ Application.DocumentManager.MdiActiveDocument.Editor Dim myPR As PromptResult myPR = ed.Drag(Me) Do Select Case myPR.Status Case EditorInput.PromptStatus.OK Return myPR Exit Do Case EditorInput.PromptStatus.None Return myPR Exit Do Case EditorInput.PromptStatus.Other Return myPR Exit Do End Select Loop While myPR.Status <> EditorInput.PromptStatus.Cancel Return Nothing End Function Protected Overrides Function Sampler(ByVal prompts As JigPrompts) _ As SamplerStatus Dim myPPR As PromptPointResult myPPR = prompts.AcquirePoint(myOpts) Dim curPos As Point3d curPos = myPPR.Value If curPos.IsEqualTo(BasePt) Then Return SamplerStatus.NoChange Else myMatrix = Geometry.Matrix3d.Displacement( _ BasePt.GetVectorTo(myPPR.Value)) BasePt = myPPR.Value Return SamplerStatus.OK End If End Function Protected Overrides Function Update() As Boolean myBRef.Position = BasePt Return False End Function End Class

Thats it. Now, we can Jig a block to help us insert a Block.

14

Hands-On Jigging Creating Jigs using VB.NET

Expanding your jig to meet your needs


We have seen a few examples of modifications we can make to display different geometry based on a few conditions. As this is an introduction to Jigs, we are not going to cover the gamut of Jigging. The main thing we need to keep in mind is that if we can dream it, we can create it. Identify what you want the jig to be like, how you want it to behave. Map out when various geometry elements are displayed and how they are displayed, and then begin the process of making it happen. It will take some time to get things just right but with some determination, you can do it.

Jig Q&A
Ask away, my friend. We may even create a Jig or two based on questions during Q&A.

Review
Creating a basic Jig is fairly straight forward especially if you have a project to begin with. When you can make modifications to it to meet your needs, you are well on the path of making a big difference in your organization by creating and implementing Jigs.

Your friend, Jerry Winters jerryw@vbcad.com

15

Hands-On Jigging Creating Jigs using VB.NET

16

You might also like