Category: Autocad .Net API

This documents the work I’ve done on the Autocad API.

I will be posting code and helpful methods which you might find of use.

Thank you!

  • The importance of identifying the assumptions Behind Every Problem

    My estimable colleague Rafik Ben made a blog post concerning a problem.

    For reference, here it is: http://routetomastery.com/blog/2017/01/08/has-pair-with-some-problem/

    So what’s so good about Rafi’s problem?

    Well like most things in the world, the problem, is not the actual problem. When it comes to computer science, the *actual* problem lies in identifying or understanding it. And the assumptions one makes are no less important.

    Let’s see this in example:

    > A man and his son are driving in a car one day, when they get into a fatal accident. The man is killed instantly. The boy is knocked unconscious, but he is still alive. He is rushed to hospital, and will need immediate surgery. The doctor enters the emergency room, looks at the boy, and says…

    > “I can’t operate on this boy, he is my son.”

    > *How is this possible?*

    It’s one of those things where you either have the answer instantly or you will never get it. It’s because your underlying assumptions and expectations were hidden and/or incorrect. The thesis of this post is this: identify the assumptions inherent behind every problem. For in doing so, you will be more likely to solve it successfully.

    Assumptions area always inherent in a problem

    Every problem has its own assumptions. And these assumptions will drive very different solutions. For example, in the post Rafi made:

    • what if one assumes that the input into the “Pair with sum” function was a disorered list vs ordered list?
    • What if one assumed that the input contained invalid data?
    • Each assumption would in turn require it’s own unique solution. And those solutions might be drastically different. It should be remembered that not all solutions were created equal.

     Assumptions to consider when formulating algorithms

     Some assumptions which I feel are important when considering problems:

    1. What are the domain/range of the inputs and outputs? (e.g. positive or negative, above zero or less than 400)
    2. What are the characteristics of the inputs/outputs? (i.e. divisible by 2, integers only, irrational numbers?).
    3. The data structure(s) of the inputs/outputs?
    4. Can you think of a structure that will make your algorithm more efficient.
    5. Is near enough, good enough? What are the costs of inaccuracy? Can we later fix inaccuracies?
    6. Time limitations?
    7. Computational power limitations?
    8. Human factors:
    • Remember, algorithms are created for the user. You might have to compromise on efficiency and/or accuracy in order to meet this need.  For example: I am positively sure that Google can do a  lot better in its search results: but the boffins there are acutely aware that it’s bad business if the user is left waiting for a search result for more than a few seconds. Consequently, there are some compromises on accuracy in favor of speed. These factors are essential when considering a solution to a problem.

    Another human related issue: the problem must be understandable to the coder, and easily maintainable:

    1. Can you easily understand the problem by looking at it? Easily understood code makes maintenance a breeeze.
    2. What parts do you think will change?

    Summary

    Understand the problem and its assumptions, and you’re halfway there.

    Here is a gist basically listing the above:

    https://gist.github.com/BKSpurgeon/69f624f959e80a7842a2a319d797f120

  • Associative functionality in the Autocad .net API

    Associative functionality in the Autocad .net API

    Question: What is the most exciting thing since sliced bread?

    The answer is AutoCad’s associative framework. It’s been two years since it’s been released, and it’s arguably one of the most powerful features of AutoCAD which has now been exposed to .net. You’d hardly know it exists because hardly anyone seems to talk about it in the forums. Anyways, I”m really excited about this new tool and I hope to be walking you all through it in the upcoming few weeks.

    There’s a tutorial out there:

    http://au.autodesk.com/au-online/classes-on-demand/class-catalog/2014/autocad/sd5217

    but I suspect it might be too hard-core for most of you so I really want to simplify it and walk you through the process. I hope you’ll derive as much pleasure from devouring it, as I will have producing it.

    What is the benefit?

    It’s just really really cool

    Ben

  • c# .Net Autocad plugin – “Hello world” Walk through

    There’s not much out there in the way of introductions. You’d have to wade through some manuals and it can be tedious. A simple walk through of how to get started. You would do well to record it at 1.5-x2 playback speed.

     

    Autocad .net c# Plug in – Hello World Example from Tek1 on Vimeo.

  • Checking the type of an Entity? (AutoCAD .net)

    Ok, so you’ve got a bunch of entities in a collection. You only want to deal with circles. You need to iterate through the collection and consider only the circles. But how will you identify the circles from the other objects.

     

    1. Casting.

    You can cast

    Entity en = en as Circle

    And then you can test whether entity is null.

    If (en == null )

    { // throw new Exception etc. etc. }

     

    Or you can try the equivalent:

    If (en is Circle)

    { // Perform operation etc. etc.}

     

    What is the catch with this approach?

    • The benefits are that it is really quick and dirty.
    • ………most important that you gotta watch out for is that it tests for Circles and subsequent sub classes of circles. You may not want that so watch out!

     

    1. GetType

    I’ve also seen folks on the forums use the Gettype to  check for the type of the entity. It goes something like this:

    en.GetType() == typeOf(Circle)

     

    The Catch with this approach

    • It’s painful to read.
    • Two computations involved, just like the first approach. I can’t see the performance being too much better or worse.

     

    Another approach is to use Dxf codes to check for the name. But this is overcomplicated. I don’t see many people using it on the forums and you need the object ID of the relevant entity and all the overhead associated with it.

    In my opinion, keep it simple. Casting, after all things considered, is probably the best option, but you have to watch out – all subclasses will return true. So you need to use the most granular class you can if that is at all important.

  • Get SelectionFilter by block Name

    It is very common that you will need to create selection filters for different types of blocks. Why repeat yourself?

     

    There is a very simple utility that I wrote that is amazingly handy for solving this very issue. Best part is that it accepts wildcards, so you can search for block references which, for example, start with Fire by passing in “FIRE*” as the argument:

    public static SelectionFilter GetSSFilterBlockReferenceByName(string name)
    {
    TypedValue[] filterlist = new TypedValue[2];
    filterlist[0] = new TypedValue(0, “INSERT”);
    filterlist[1] = new TypedValue(2, name);
    SelectionFilter filter = new SelectionFilter(filterlist);
    return filter;
    }

    So simple!

  • Convert ObjectID[] to ObjectIDCollections!

    When dealing with selection sets we can obtain the object ids of the objects contained within. The method though, returns an array.

     

    But what if we want an ObjectIDCollection?

     

    We can simply pass the ObjectID[] array into the ObjectIDCollection constructor.

     

    Simple. The last thing you want to do is iterate through the array and add it to a collection individually. A simple yet handy hint which can save you a lot of effort.

  • Batch Processing AutoCAD drawings using AcCoreConsole and Script Pro 2.0

    Batch Processing Using AcCoreConsole is now here.

    What is AcCoreConsole?

    • It is a command line version of AutoCAD without a user interface.
    • It allows us to batch process AutoCAD drawings super fast.

    Example:

    • Let’s say I have 500 drawings. (This is a prime candidate for batch processing).
    • Now let’s say I have to apply a tedious and repetitive change to each of those drawings.
    • This would take a long time to do manually. You would have to employ a CAD monkey to do this. No more! You can automate everything!

    Watch the Video: It is will explain the technical stuff below:

     

    For Geeks Only:

    • If you have any questions, please feel free to ask!

    Geek-speak: How does it work? (It might not make sense to most of you guys but that’s ok – all you need to know is that you CAN automate things if you want to):

    1. Write a custom .net DLL (or ARX if you prefer).
    2. Write a script file to load this dll and to apply it to a drawing, and to add the parameters your dll requires.
    3. Apply that particular script file to all 500 of your drawings using Scrip Pro 2.0.
    4. Use AcCoreConsole to do the heavy lifting. It is light weight and fast and ideally suited to batch processing.
    5. Make yourself a cup of coffee – and by the time you are done, your files will have been updated.
    6. Check the log files to ensure everything went smoothly.
    7. Important: Ensure you DLL is in a “trusted location”.

    Here is my ScriptFile:

    (Remember when pasting that you have to add the .scr extension)

    My .net DLL:

    • Just a standard DLL which changes the attribute values of a particular block. You’ll have to get rid of the pesky reference to AcMgd but otherwise it is basically the same as the below. Kean says it better than I could ever say it.
    • Of course you will have to change the above script file to suit the DLL command name, and the DLL location, as well as the particular block name and tag and new attribute values that will be particular to your project.
    •  You can access the Document with the following code:

    Document acDoc =  Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;

    • Here is Kean’s code.

     

    If you have any questions: Go ahead and call me!

  • Introducing AcCoreConsole

    AcCoreConsole is AutoDesk’s brand new toy. It is basically a command line version of AutoCAD. It has no “User Interface”. That means that if you are a user, there are no menus to click on. No buttons. Nothing. You have to do everything via text. Via command line, like it was in the very beginning.

    You cannot see lines.

    What is the benefit of this you ask? Well it allows for super fast batch processing.

    Please watch my screencast for a very brief introduction to this new technology. I will provide another video of the batch processing power of this babey.

     

    https://knowledge.autodesk.com/community/screencast/d6ef5929-4208-443d-8b53-eca0c637328a

    I hope you learn something.

     

    Ben

  • Autocad Plug-in: Creating Order Forms Automatically

    Normally creating order forms are a nightmare.

    How does everyone else create order forms?

    I have no idea how people do it. They would have to manually count/iterate through every single block which exists in the model space and simultaneously note down the parts’ associated panel number(s). They could do it on their PC, or they could do it on paper. And the worst part is, is that it would distract them from doing some real work. (Or work that they would rather be doing).

    Trust me – I’ve been doing monkey work like this since I was a kid. Counting panels, counting beams, counting parts: it really is a crime against humanity. Not only is it painfully tedious, it’s error prone: it’s like trying to count the stars in the sky: it is very easy to lose count, to lose your place, or to make mistakes.

    Automatic Order Forms Demo – The power of the Autocad API customisation

    The power of the Autocad .Net API breaks the back of it for you. I begged the head boss to let me release this to the public, just to give you an idea of the cool things that the API can do. I now present to you automated order forms: no more counting. No more errors. All done in less than 5 minutes (as opposed to five hours).