Inserting a block via the command line

Inserting a block via the command line

AlexBern44
Advocate Advocate
8,518 Views
18 Replies
Message 1 of 19

Inserting a block via the command line

AlexBern44
Advocate
Advocate

Dear AutoCAD professionals, a question about inserting blocks into the drawing.

Is it possible to insert a block with a predefined name and path to the folder where it is stored? By using only the command line?

For example this command could look like this:

insert; "C:\USERS\USER\DESKTOP\DRAWING.DWG"

This is just to illustrate what I mean.


Thanks!

0 Likes
Accepted solutions (3)
8,519 Views
18 Replies
Replies (18)
Message 2 of 19

BeKirra
Advisor
Advisor
Accepted solution

Yes, it can be done. Here is an example and by doing this it will lead you to AutoLisp world... 😀

 

(setq pt (getpoint "\nSelect a point to insert block:"))(command "insert" "C:\\USERS\\USER\\DESKTOP\\DRAWING.DWG" pt "" "" "")(princ)

 

HTH

 

Please mark "Accept as Solution" and "Like" if my reply resolves the issue and it will help when others need helps.
= ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ =
A circle is the locus of a cursor, starting and ending at the same point on a plane in model space or in layout such that its distance from a given coordinates (X,Y) is always constant.
X² + Y² = C²
Message 3 of 19

AlexBern44
Advocate
Advocate

Hi BeKirra

I tested your solution and it works. This is what I was looking for.

But I still have a question, now for a solution.
While inserting a block, when the program asks Select a point to insert block, I am not seeing the insert object. While, with the standard insertion of blocks, we see an object near the cursor.
Is it possible to do the same, now with this?

Thank you🙂

0 Likes
Message 4 of 19

imadHabash
Mentor
Mentor

>> when the program asks Select a point to insert block, I am not seeing the insert object.

inserted block will noticed in both way . try to scroll your mouse wheel to zoom in to see your inserted block.

 

 

Imad Habash

EESignature

0 Likes
Message 5 of 19

AlexBern44
Advocate
Advocate

.

0 Likes
Message 6 of 19

AlexBern44
Advocate
Advocate
imadHabash,

I do as you say, but I don't watch the block. It appears on the screen only after specifying the insertion point.
0 Likes
Message 7 of 19

imadHabash
Mentor
Mentor

>> It appears on the screen only after specifying the insertion point.

This is how AutoCAD work .. it's normal . 

Imad Habash

EESignature

0 Likes
Message 8 of 19

AlexBern44
Advocate
Advocate
I understand. But I wanted the block to be also visible when the insertion point was selected, even before the insertion, as is the case with a normal block insert.
Could it be possible to inject a specific parameter into the code that BeKirra has kindly provided to make it possible?
0 Likes
Message 9 of 19

Kent1Cooper
Consultant
Consultant
Accepted solution

@AlexBern44 wrote:
.... I wanted the block to be also visible when the insertion point was selected, even before the insertion, as is the case with a normal block insert. ....

You need to be in an INSERT command at the time to get that, not in some generic get-a-point function that could be applicable to anything.  [When (getpoint) is asking for a point, AutoCAD has no way of knowing what you're going to use it for.]  Try it this way, with the pause operator at the place in the command where User input is required [in this case the insertion point]:

 

(command "_.insert" "C:\\USERS\\USER\\DESKTOP\\DRAWING" pause "" "" "")

 

It will supply the usual prompt, so you don't need to spell out what you're asking for, as you do when using (getpoint).  [By the way, note that you don't need to include the filetype ending, though it doesn't hurt.]

 

Or in command-macro format that you could put into a Tool Palette item, where a backslash is used for where User input is required [which is why you need to use forward slashes for filepath dividers]:

 

^C^C_.INSERT C:/USERS/USER/DESKTOP/DRAWING \;;;

Kent Cooper, AIA
0 Likes
Message 10 of 19

AlexBern44
Advocate
Advocate
Kent1Cooper,

I think I understand now.
Could you show please with an example of that code how it might look? I am having more difficulty with these, since I do not understand anything in Autolisp.

(setq pt (getpoint "\nSelect a point to insert block:"))(command "insert" "C:\\USERS\\USER\\DESKTOP\\DRAWING.DWG" pt "" "" "")(princ)

Many thanks
0 Likes
Message 11 of 19

Kent1Cooper
Consultant
Consultant
Accepted solution

@AlexBern44 wrote:
....
Could you show please with an example of that code how it might look? ....

The first line of code in Message 9 should be all you need.  You could just copy that and paste into the command line, and edit the filepath and name appropriately.  Beyond that, it can be defined into a command that you can give a name, if you want, or put into a Tool Palette item either in that AutoLisp form or the macro variety in Message 9.  Tell us more about exactly how you prefer to use it.

Kent Cooper, AIA
0 Likes
Message 12 of 19

AlexBern44
Advocate
Advocate
Kent1Cooper,
I checked this one more time. This works great.
Thanks!
0 Likes
Message 13 of 19

BeKirra
Advisor
Advisor

@AlexBern44 wrote:
imadHabash,

I do as you say, but I don't watch the block. It appears on the screen only after specifying the insertion point.

 

The code in post #2 shows the basic (idea) of AutoLisp while @imadHabash's code in post #9 is more effective.

The "pause" holds the insert function until user selects an insertion point.

You are able to see the block when moving the mouse when using his code.

 

FYI

(setq pt (getpoint "\nSelect a point to insert block:"))
(command "insert" "C:\\USERS\\USER\\DESKTOP\\DRAWING.DWG" pt "" "" "")
(princ)

 

The code in post #2 (or above) doesn't show you the block before the code is ended. No matter you zoom in or out.

The 1st portion of the code asks user select a point as command "insert" requires.

Thus this code logically doesn't search or load the target block before user select an insertion point.

The last portion "removes" any "nil" message from command line when command "insert" is complete.

And the code acts slightly different to manually insert block to drawing.

 

Hope this explains.

Please mark "Accept as Solution" and "Like" if my reply resolves the issue and it will help when others need helps.
= ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ =
A circle is the locus of a cursor, starting and ending at the same point on a plane in model space or in layout such that its distance from a given coordinates (X,Y) is always constant.
X² + Y² = C²
0 Likes
Message 14 of 19

AlexBern44
Advocate
Advocate

Thank you for this complete answer.
I would like to ask another question on this topic. Perhaps you are familiar with ways to mirror or rescale the block while it is being inserted. That is, to change the direction of the block relative to the insertion point when it is already visible on the screen but not yet inserted in the drawing. Maybe there are some keyboard shortcuts that can be used to make a mirror or to change the scale.

0 Likes
Message 15 of 19

Kent1Cooper
Consultant
Consultant

@AlexBern44 wrote:

.... ways to mirror or rescale the block while it is being inserted. That is, to change the direction of the block relative to the insertion point when it is already visible on the screen but not yet inserted in the drawing. ....


Read about the -INSERT command [note the hyphen prefix] in Help, and/or just type it in and look at the options -- you can scale it in any or all directions, rotate it, and some other things, before dragging it into place.  If you have such options that you use regularly, you can build them into the command sequence inside an AutoLisp (command) function.

Kent Cooper, AIA
Message 16 of 19

BeKirra
Advisor
Advisor

@AlexBern44 wrote:

Thank you for this complete answer.
I would like to ask another question on this topic. Perhaps you are familiar with ways to mirror or rescale the block while it is being inserted. That is, to change the direction of the block relative to the insertion point when it is already visible on the screen but not yet inserted in the drawing. Maybe there are some keyboard shortcuts that can be used to make a mirror or to change the scale.


 

You are welcome.

You can have a look into HELP to learn more about the command "insert" as @Kent1Cooper suggested.

But...

IMO the questions that you arose now are "off the topic". It is not recommended to copy a piece of "long" code to command line.

My early reply in post #2 was showing the basic and "dirty" solution for your question. Actually I am afraid it is not practical in real life because the "code" can not be repeated when you want to insert another block file.

If you do so, you might have to head into AutoLisp and create a new command that allows you to insert varies blocks.

In your code you might have to provide the following user input options:

1. block name and it is location.

2. "mirror" or "not".

3. "change insert scale" or accept 1:1 as default.

 

BTW You can also browse the AutoLisp forum (see the link below) if you are really happy to learn more about AutoLisp.

Visual LISP, AutoLISP and General Customization - Autodesk Community

 

HTH and welcome onboard of AutoLisp. 😁

 

Please mark "Accept as Solution" and "Like" if my reply resolves the issue and it will help when others need helps.
= ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ =
A circle is the locus of a cursor, starting and ending at the same point on a plane in model space or in layout such that its distance from a given coordinates (X,Y) is always constant.
X² + Y² = C²
Message 17 of 19

AlexBern44
Advocate
Advocate

/

0 Likes
Message 18 of 19

AlexBern44
Advocate
Advocate

Dear Kent1Cooper and BeKirra


I have carefully read your answers as well as the description of the command in the HELP.
As I now understand, the actions with the block, that I want to implement, do not have standard commands in Autocad.
The -insert command only includes the ability to enter rotation or scale parameters on the command line.
The other option is to create code in Autolisp, which really wouldn't be practical as you pointed out.

Just to be clear, I will repeat my question with an example. My intent was this. I was expecting that there is some key combination that allows to make changes with a block that is already on the screen, in the process of insertion.
After all, this is fairly practical, let's take this example, key combinations "Ctrl +" could increase the scales by one step, "Ctrl + R" rotate by 1 degree, "Ctrl + X" mirror relative to the axis X, "Ctrl + Y" relative to the axis Y and so on.
This is quite practical, a little strange that there is no such thing🙂

0 Likes
Message 19 of 19

Kent1Cooper
Consultant
Consultant

@AlexBern44 wrote:

.... I was expecting that there is some key combination that allows to make changes with a block that is already on the screen, in the process of insertion.

After all, this is fairly practical, let's take this example, key combinations "Ctrl +" could increase the scales by one step, "Ctrl + R" rotate by 1 degree, "Ctrl + X" mirror relative to the axis X, "Ctrl + Y" relative to the axis Y and so on.
....


I disagree that it's practical [see my bracketed question in what follows].  The "key combination" is to type in the abbreviation for one of the command-line options in the -INSERT command.  Instead of Ctrl+ to scale it up by "one step" [who gets to decide what ratio "one step" is? and what if you want a scale factor that's between those "steps"?], you type in S for the Scale option [all axes together], and tell it directly exactly how much you want to scale it by.  Instead of CtrlR to rotate it by 1° [what if you want to rotate it by something that's not whole degrees? and do you have to hit CtrlR 180 times to spin it half-way around? and do you need a separate one to rotate it in the other direction?], you type in R for the Rotate option, and tell it directly exactly how much you want to rotate it.  Instead of CtrlX to mirror it in the X direction, you type in X for the X scale factor option, and tell it -1 [assuming you don't also want to change the size, but you can do that, too].  Etc., etc.  All changes made in these ways go into effect immediately in what you see on-screen, so you can have it in its final scale and orientation and so on, as you drag it around to give it the insertion point.

Kent Cooper, AIA
0 Likes