Skip to content

Instantly share code, notes, and snippets.

@benkoshy
Last active January 15, 2023 14:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benkoshy/9e02c5715b79e4afd21465ceb55f3e2f to your computer and use it in GitHub Desktop.
Save benkoshy/9e02c5715b79e4afd21465ceb55f3e2f to your computer and use it in GitHub Desktop.
What is a jig? AutoCAD .net API (c#)

Understanding Jigs

A video demo which will help give you a holistic view of what a jig is, and its power.

The Client

Let’s start with the client class. If you don’t understand this terminology please see a short explanation below this article.

Without commentary – The structure of a jig (within the client):

DrawJigWithDynDim jig = new DrawJigWithDynDim(basePt, 500, 500); 
if (jig.DragMe() == PromptStatus.OK)
{

}

With commentary:

	// instantiate the custom jig object. We are the owners of the 
	// DrawJigWithDynDim class – it’s our class, not AutoDesk’s.

DrawJigWithDynDim jig = new DrawJigWithDynDim(basePt, 500, 500); 

	// DragMe() method allows you to move your mouse around and to 
	// preview various shapes and sizes of proposed entities  that will
	// be drawn on your monitor. This method returns a PromptStatus 
	// enum when complete.

if (jig.DragMe() == PromptStatus.OK)
{
	// If everything went to plan we can draw the finalised dimensions 
	// that the jig determined, into the model space. 
	// We can do this inside the above if statement.
	
	// We could even put this code in the server class, rather than 
	// here, directly, in the client. Why should the client be responsible
	// for how something is drawn? E.g. something like jig.DrawMe(); could be added as a method to the jig server class.
}

	// Code which actually adds the final shape that the jig represented, 
	// is then added into the model space. Perhaps this should more 
	// appropriately be handled within the jig (server) itself, rather than in the client – but hey – I didn’t write the class.

The server – Let’s look inside the Jig

Now let’s think about how a jig should work, given what we know about it:

  • If we move a mouse, or press a key, then the appearance of the jig should change. For example, when we increase the radius of a circle (taking the circle jig as an example) then the circle that’s drawn on the screen gets bigger and bigger etc.

  • When the jig is over, then the final dimensions and geometric properties of the circle are saved – and it’s all added to the model space.

Without commentary:

    public class DrawJigWithDynDim : DrawJig
    {
        public DrawJigWithDynDim(Point3d pt, double w, double h)
        {
        }
        public PromptStatus DragMe()
        {            
	    while ((res = ed.Drag(this)).Status == PromptStatus.Other) ;
            return (res.Status == PromptStatus.None) ? PromptStatus.OK : res.Status;
        }
        protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
        {
        }
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
        }
    }

With commentary:

    public class DrawJigWithDynDim : DrawJig
    {
	// Store the location data that you need as fields in this class.
	// When the jig has finished being successfully dragged then
	// you need to be able to access the data behind these fields
	// from the client class (i.e. the calling class) so that
	// you can actually draw what you want to draw.
	// I don't like doing this, but it seems to be the practice
	// that most .net developers follow.

        public DrawJigWithDynDim(Point3d pt, double w, double h)
        {
        }

        public PromptStatus DragMe()
        {
	    // Did the user successfully complete the jig etc., or did 
	    // the user escape from the command? 
            
	    while ((res = ed.Drag(this)).Status == PromptStatus.Other) ;
            return (res.Status == PromptStatus.None) ? PromptStatus.OK : res.Status;
        }

        protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
        {
		// The code here is used to actually draw the shape into the 
		// model space. But when and how often is the shape actually 
		// drawn? That is dependent upon how far the mouse moves. 
		// This words hand in hand with the SamplerStatus Sampler 
		// (JigPrompts prompts) method. Please refer to the explanation
		// contained in that method.
		
		// When the user moves the mouse then whats drawn on your screen
		// might need to be updated. But what exactly is drawn on your
		// screen? That is determined within this method: what is drawn
		// and how it is drawn, is determined here.
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
	     // You receiver a JigPrompts object. Using this object you can know 
	     // where the user’s cursor is. And you can determine: (i) whether 
	     // there is a need to redraw the jig or not, as well as (ii) the 
	     // types of inputs you require in your jig. (You might need to 
	     // add a case statement if you want your jig to have many different 
	     // stages where different types of inputs are collected from the user). 
 
            // Every couple of micro seconds, you can check whether the user 
	    // has: (i) moved the mouse, or whether they’ve finished the jig
	    // and finalised the geometries wanted, or whether they’ve pressed 
	    // cancel. If you’ve moved your mouse by a certain amount (as determined
	    // by the programmer within this very method) then you can 
	    // ask your new geometries to be redrawn – but only if you the
	    // programmer have determined whether there is a change. In other 
	    // words, if the mouse has only moved 0.001 units then this is a
	    // very small amount – there is no need to redraw the entire jig 
	    // given it’s hardly moved. Accordingly you can return a 
	    // SamplerStatus.NoChange to indicate to AutoCAD that there is
	    // no need to update the geometry of the drawn jig.
	    
	// Accordingly, while the geometry is redrawn, you can
	// also take the opportunity here to update the 
	// geometry associated with the shapes/figure that you
	// are trying to draw, in the model space.
        }
    }

Here is the original thread used a source for this tutorial: original Thread

Client / Home class distinction:

	// In this case the client is the `Home` class, and the server class would be the `Person` class.
Class Home
{
	private static void Main(string[] args)
        {
            Person mum = new Person();
            mum.VisitMoreOften();
        }
}
@bmomen
Copy link

bmomen commented Sep 21, 2020

Thanks for this tutorial. I am wondering if its possible for a jig to apply during the editing of a feature, not just the creation. So in your video example, would it be possible to move the curve and for the dimensions to update automatically? I'm convinced this would require a different mechanism with custom objects and subMoveGrip overrides. Thank you

@benkoshy
Copy link
Author

@bmomen - for dynamic dimensions - where you edit a complex object and the dimensions update automatically - you would have to use ObjectArx. Just search "dynamic dimensions ObjectArx" or ask in the forums and someone would point you in the right direction.

Having said that you can edit an object using a jig using .net - it can be done. As far as automatically updating the dimensions: here's what I would do: rather that update the dimensions, I would simply force/or ask the users to completely re-create the dimensions from scratch, if that was at all possibly, given the complexities of the particular object you are trying to dimension, and the time / costs of writing custom code to do that. my two cents.

@bmomen
Copy link

bmomen commented Sep 21, 2020

@BKSpurgeon - You've confirmed my suspicions that it is indeed possible however due to the complexity of the particular object, it is probably best to go down the ObjectARX route (something we are trying to avoid). Thanks for the swift response.

@benkoshy
Copy link
Author

benkoshy commented Sep 21, 2020

@bmomen - "You've confirmed my suspicions that it is indeed possible"

Do you mean it's possible to do dynamic dimensions to edit the dimensions of a complex object that is already created - roughly how would this be done in .net?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment