Author Topic: Insert dynamic block from menu  (Read 2663 times)

0 Members and 1 Guest are viewing this topic.

like_citrus

  • Newt
  • Posts: 114
Insert dynamic block from menu
« on: January 18, 2021, 11:57:30 PM »
I've used the code below to insert the contents of another drawing as a regular block (eg, adding a joist/beam section). The first part is from an MNU file which references "BlockInsert" which is a LISP file set on auto-load.
"BlockInsert" takes the file name and then adds the date to create a unique block name.
This is all good and has worked fine.

The next step I want to take is to insert dynamic blocks from the MNU file. At the moment I'm looking at adding bolts and want to have the length as a variable.
(1) Firstly, I want to know if it's possible to add a dynamic block with code - in a way that the source file only consists of lines and arcs, and no intelligent information such a dynamic block would have. I'm guessing the answer is No, so maybe just forget about this question.
(2) I would then have a source file with a bolt as a dynamic block. How can I just add the dynamic block ... at the moment, the dynamic block would be made inside a regular block, which I don't want. The thing is though, I would like to change the name of the dynamic block. The reason is that I've had issues with block before with the same name, as changes have carried through all block with the same name when I didn't intend this to occur.



Code: [Select]
[->UB]
   [610UB125]^C^C(BlockInsert "UniversalBeams_610UB125");scale;1;
   [610UB113]^C^C(BlockInsert "UniversalBeams_610UB113");scale;1;
   [610UB101]^C^C(BlockInsert "UniversalBeams_610UB101");scale;1;
[--]

Code: [Select]
(defun BlockInsert (blkname)
(setq time (rtos (getvar "CDATE") 2 6)) ; Format YYYYMMDD.HHMMSS
(setq year (substr time 3 2))
(setq month (substr time 5 2))
(setq day (substr time 7 2))
(setq hour (substr time 10 2))
(setq minutes (substr time 12 2))
(setq seconds (substr time 14 2))
    (command "_.-insert" (substr (strcat blkname "-" year month day hour minutes seconds "=" blkname) (+ (vl-string-position (ascii "_") blkname 1) 2)) pause 1 "" 0)
)



Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Insert dynamic block from menu
« Reply #1 on: January 19, 2021, 04:16:01 PM »
Note that this:
Code - Auto/Visual Lisp: [Select]
  1.         (setq time (rtos (getvar "CDATE") 2 6)) ; Format YYYYMMDD.HHMMSS
  2.         (setq year (substr time 3 2))
  3.         (setq month (substr time 5 2))
  4.         (setq day (substr time 7 2))
  5.         (setq hour (substr time 10 2))
  6.         (setq minutes (substr time 12 2))
  7.         (setq seconds (substr time 14 2))
  8.     ... year month day hour minutes seconds ...

Can become:
Code - Auto/Visual Lisp: [Select]
  1. (menucmd "m=$(edtime,0,yymoddhhmmss)")

When you state:
Quote
I would then have a source file with a bolt as a dynamic block. How can I just add the dynamic block ... at the moment, the dynamic block would be made inside a regular block, which I don't want.

Why not have the source file contain only the dynamic block definition? i.e. WBLOCK the dynamic block definition to its own file such that, when inserted, the dynamic block is not nested.

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: Insert dynamic block from menu
« Reply #2 on: January 19, 2021, 07:32:17 PM »
A dynamic bolt with length and size YES its out there.

In lisp pick point, do rotation then enter length and size. Use Lee's dynamic block lisp to update variables.






A man who never made a mistake never made anything

like_citrus

  • Newt
  • Posts: 114
Re: Insert dynamic block from menu
« Reply #3 on: January 19, 2021, 09:40:31 PM »
Thanks for the responses.

Lee Mac, your code works actually and is much more efficient, thanks for that.
For the issue of "not" having a nested block, with the original source file, it's actually the case that there is only the dynamic block definition. But the code makes it into a block. So with my other source files which are only lines/arcs, nothing else, the end product is still a block.
I can't make the code work such that only the original contents are inserted, not as a block.
In my notes I had written that "1" represents the scale and "0" is the rotation for the block. I deleted these but still got a block. cdate, strcat, substr don't appear to relate to blocks.

Code: [Select]
(defun BlockInsert (blkname)
    (command "_.-insert" (substr (strcat blkname "-" (menucmd "m=$(edtime,0,yymoddhhmmss)") "=" blkname) (+ (vl-string-position (ascii "_") blkname 1) 2)) pause 1 "" 0)
)


BIGAL, is the block and LISP from the Lee Mac website as I could not find it anywhere.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Insert dynamic block from menu
« Reply #4 on: January 20, 2021, 08:28:17 AM »
...with the original source file, it's actually the case that there is only the dynamic block definition.

Just for completeness: do you mean that when you open the source drawing it prompts you to open it in the Block Editor?

like_citrus

  • Newt
  • Posts: 114
Re: Insert dynamic block from menu
« Reply #5 on: January 20, 2021, 09:47:19 PM »
Lee Mac, the source drawing isn't actually opened at all.

I've attached a sample set of files to show how the menu is loaded and blocks inserted. I couldn't test because for some reason the previous menu was reinstalled. I tried MENUUNLOAD but it didn't work.
- Save the folder in C drive.
- In AutoCAD, type SCRIPT. Load the script file "STRUCTURAL_STEEL.scr".
- Restart AutoCAD and you should have a menu file.

Click on any item and it inserts it into the current drawing.
It references the LISP routine "BlockInsert" which is set to auto load.

The thing that happens is that the source data is inserted as a block. But if I have a dynamic block as the source file, such as "Bolts_DYNAMIC BOLT", I don't want a block as it's already a block. For this I would call up another LISP routine.
This LISP would just enter the source file. If possible a name change would be better because I've had issues with blocks using the same name before. The file name with a date stamp is OK.


SCRIPT FILE
Code: [Select]
(if (and (not (findfile "STRUCTURAL_STEEL.mnu"))
(findfile "C:\\POP12 StrSt\\STRUCTURAL_STEEL.mnu")
)
(setenv "ACAD" (strcat (getenv "ACAD") "C:\\POP12 StrSt"))
)
(command "._MENULOAD" "STRUCTURAL_STEEL.mnu")
(menucmd "P12=+STRUCTURAL_STEEL.CTOPopSTRUCTURAL_STEEL")


MNU (menu) FILE
Code: [Select]
***MENUGROUP=STRUCTURAL_STEEL
***POP12
**CTOPopSTRUCTURAL_STEEL
              [Structural Steel]
              [--]
              [->Beams]
                 [530UB82.0]^C^C(BlockInsert "UniversalBeams_530UB82.0");scale;1;
                 [360UB50.7]^C^C(BlockInsert "UniversalBeams_360UB50.7");scale;1;
                 [<-150UB18.0]^C^C(BlockInsert "UniversalBeams_150UB18.0");scale;1;
              [--]
              [->Bolts]
                 [M12]^C^C(BlockInsert "Bolts_M12");scale;1;
                 [M20]^C^C(BlockInsert "Bolts_M20");scale;1;
                 [M36]^C^C(BlockInsert "Bolts_M36");scale;1;
                 [<-DYNAMIC BOLT]^C^C(BlockInsert "Bolts_DYNAMIC BOLT");scale;1;   
              [--]

LISP TO INSERT SOURCE FILE
Code: [Select]
(defun BlockInsert (blkname)
    (command "_.-insert" (substr (strcat blkname "-" (menucmd "m=$(edtime,0,yymoddhhmmss)") "=" blkname) (+ (vl-string-position (ascii "_") blkname 1) 2)) pause 1 "" 0)
)





Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Insert dynamic block from menu
« Reply #6 on: January 21, 2021, 03:56:35 PM »
Lee Mac, the source drawing isn't actually opened at all.

No, but if you were to open the source drawing for the dynamic block manually in AutoCAD, if it contains only the Dynamic Block definition (i.e. if it is itself the Dynamic Block definition) it should present you with the prompt 'Open in Block Editor'. On downloading your zip file and opening the drawing Bolts_DYNAMIC BOLT.dwg, I can see that this drawing contains a reference of the dynamic block, not the dynamic block itself - you should WBLOCK the definition of the dynamic block to a separate file (which will then issue the aforementioned prompt if you were to open it in AutoCAD).

Since your existing drawing contains a reference of the dynamic block, such reference then becomes nested when the source drawing is inserted into another drawing as a block.

like_citrus

  • Newt
  • Posts: 114
Re: Insert dynamic block from menu
« Reply #7 on: January 21, 2021, 10:05:54 PM »
OK thank you but I'm more confused now.

1. How can you tell that Bolts_DYNAMIC BOLT.dwg is a reference and not the dynamic block itself. At this point I don't really understand the purpose the question apart from knowing how you could tell the difference.
2. That's the second time you mentioned WBLOCk but I still don't understand the concept. One of the things it does is it takes the elements and places them in a separate file. But I already have a separate file. I tried WBLOCk on Bolts_DYNAMIC BOLT.dwg to create another file, and inserted again from the menu but could not see any difference. I didn't get the prompt "Open in Block Editor".
3. With your last note that the existing drawing contains a reference and then it becomes nested ... with the other reference drawings such as 530UB82.0, these are just lines and arcs but they become a block when using the routine "BlockInsert". With the dynamic block, I was hoping to call up another routine where you don't insert as a block, just whatever is there. But with current routine I can't see anything which tells it to make it a block, so don't know how to modify. And then there is also the issue of a unique name.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Insert dynamic block from menu
« Reply #8 on: January 22, 2021, 05:08:48 PM »
1. How can you tell that Bolts_DYNAMIC BOLT.dwg is a reference and not the dynamic block itself.

Because on opening the drawing, I am presented with a reference of the dynamic block inserted into Modelspace, rather than the components of the dynamic block definition (i.e. lines, arcs, parameters etc) displayed in the Block Editor - just as you might open the source drawing for a standard (non-dynamic) block and find the lines/arcs/etc constituting the block definition rather than a reference of the block itself.

2. That's the second time you mentioned WBLOCk but I still don't understand the concept. One of the things it does is it takes the elements and places them in a separate file. But I already have a separate file. I tried WBLOCk on Bolts_DYNAMIC BOLT.dwg to create another file, and inserted again from the menu but could not see any difference. I didn't get the prompt "Open in Block Editor".

You have a separate drawing file into which a reference of the dynamic block has been inserted, not a separate file representing the dynamic block definition.

Perform the following steps exactly, and you'll see the difference:
  • Open Bolts_DYNAMIC BOLT.dwg
  • Ensure no objects are selected
  • Invoke the WBLOCK command at the command line
  • Change the radio button to 'Block' and choose 'bolt' from the drop down.
  • Specify an output drawing for the definition (e.g. 'bolt.dwg')
After generating the new 'bolt.dwg' file, try opening this file in AutoCAD, and then try inserting this file as a block into AutoCAD (either using the standard INSERT command or your program).

3. With your last note that the existing drawing contains a reference and then it becomes nested ... with the other reference drawings such as 530UB82.0, these are just lines and arcs but they become a block when using the routine "BlockInsert".

Exactly - note that this behaviour is nothing to do with your AutoLISP program, but simply the standard behaviour of the INSERT command in AutoCAD. When using the INSERT command and selecting your drawing file, the content of the selected drawing file constitutes the block definition and a reference of such definition is inserted into the drawing at the location, scale & rotation that you specify. If the selected drawing file to be inserted contains a block reference, then such block reference will be nested within the block definition in the main drawing - this is standard behaviour for any block, not only dynamic blocks.

like_citrus

  • Newt
  • Posts: 114
Re: Insert dynamic block from menu
« Reply #9 on: January 22, 2021, 09:51:13 PM »
Thanks again for explaining further.
After following the instructions, it all seems to work out.
The dynamic block is inserted as required (not nested) and the name is unique, so this is exactly what I was looking for.
It will take a while to fully understand but there's enough notes to refer to later.
Cheers.