Monday, November 26, 2012

Autocad macro...calling coordinate points on blocks

One thing that always comes up when creating P&ID's is; "how can you make it simplier to insert blocks and not have to take the time to do all the trimming of the lines once the block is in?"
I was actually able to create a couple of ways that allowed AutoCAD LT users and full version AutoCAD users to be able to do this.  I have posted the LT version with the diesel macro, but today I am going to go over the lisp routine route.



Below you will see today's code that we are going to go over:

*^c^cattdia;0;^c^c_.insert;CHECK;\;1;\;;^C(setq edata (entget (setq en (entlast))));^c^cbreak;\f;(setq edata (entget (entnext (cdr (assoc -1 edata)))))(setq ip1 (cdr (assoc 10 edata)));(setq edata (entget (entnext (cdr (assoc -1 edata)))))(setq ip1 (cdr (assoc 10 edata)));^c^cattdia;1;^c

Okay...let's dive into this.
It isn't as overwhelming as it looks...it is actually quite simple.

So, first we are calling the attdia command and setting it to 0.  We are doing this so that the 2 attributes that are attached to the block won't show up with a dialog prompt box.

Next we start the insert command and in this example we are inserting a check valve.

CHECK;\;1;\;; - the forward slash is to prompt for user input and this is for the insertion point.
CHECK;\;1;\;; - here we are telling it what scale the block is going to be inserted at.
CHECK;\;1;\;; - This is to set the rotation of the block...another user input prompt.
CHECK;\;1;\;; - and these last two semicolons are to enter through the 2 attributes that are attached to
                           the block.

Next, we will be going into the routine that recalls the block and calls for the point locations.
^C(setq edata (entget (setq en (entlast)))) - here we are telling AutoCAD to look up the last block that was inserted.
Now we call on the break command with the fence option.
^c^cbreak;\f;

Now comes the fun part of calling for the attribute points.
(setq edata (entget (entnext (cdr (assoc -1 edata)))))(setq ip1 (cdr (assoc 10 edata)));(setq edata (entget (entnext (cdr (assoc -1 edata)))))(setq ip1 (cdr (assoc 10 edata)));^c^cattdia;1;

For both points the code is the same, so once you type it once you will be able to copy and paste for the second point, and you can see that by the semicolon that has been made bold and underlined to help you see it.

But here is the code:
(setq edata (entget (entnext (cdr (assoc -1 edata)))))(setq ip1 (cdr (assoc 10 edata)))

the first (setq) grouping is calling for the bp1 attribute in the check valve block, and then the second (setq) grouping is what returns the coordinate values of the attribute, and then establishes the first point.

Now all you got to do is add a semicolon and then copy and paste the code for the second point and then end it with a semicolon and you are done.

Happy Trimming...

No comments:

Post a Comment