Help - Routine from another lisp routine.

Help - Routine from another lisp routine.

arperezinf
Advocate Advocate
5,642 Views
34 Replies
Message 1 of 35

Help - Routine from another lisp routine.

arperezinf
Advocate
Advocate

Hi everyone! 👋

I have a lisp routine called "CD", I use this routine to insert multiple blocks. When I call it the "CD" routine in autoCAD, it asks me to enter a text, for example "AB", which corresponds to a certain block, finally it asks me the point where I want to insert the block.

 

I need to call the "CD" routine from another lisp routine.

I've been looking on the internet and with "COMMAND", it's done.

I want to insert several blocks at the same time, without having to call the lisp routine "CD" again and write the text that corresponds to the other block.

(command "._CD" "AB" ""); Block 1
(command "._CD" "AC" ""); Block 2
(command "._CD" "AD" ""); Block 3

But it doesn't work for me, I can only insert the first block.

Could you tell me or explain how I could do this.
Thank you very much for reading my message.
Greetings.

0 Likes
Accepted solutions (4)
5,643 Views
34 Replies
Replies (34)
Message 2 of 35

john.uhden
Mentor
Mentor

@arperezinf 

You can't call an AutoLisp command function with the (command ...) function.

You can call another AutoLisp command function the same way as you would call any other lisp function...

(C:mycommand) in parentheses.

Note that since we basically never create command functions to accept any argument, you can't supply it one as say

(c:mycommand "AB").  I mean you CAN, but there's no sense to it.

BUT you can make a copy of your command function but leave out the C: and add an argument name, eg.

(defun mycommand (arg / a b c) ...)

and then call it via (mycommand "AB").

Oh, and it can be in the same .lsp file.

Even better than that...

(defun c:Mycommand ( / str)
  (setq str (getstring "\nEnter block name: "))
  (mycommand str)
  (princ)
)

John F. Uhden

0 Likes
Message 3 of 35

john.uhden
Mentor
Mentor

@arperezinf 

OR, look at the use of vlax-add-cmd.

John F. Uhden

0 Likes
Message 4 of 35

Sea-Haven
Mentor
Mentor

You can call another lisp from within a lisp, if the "CD" lisp is not loaded the call will fail, a simple check is to see if "CD" is a relevant command. Load if missing.

 

 

(if (not CD)(load "myCDprogram)) ; put at start so only checked once

(command "._CD" STR "")

 

 

Message 5 of 35

john.uhden
Mentor
Mentor

NNZZZTTT.

C:CD is not an AutoCAD command until he makes it one.

John F. Uhden

Message 6 of 35

arperezinf
Advocate
Advocate

Hello @john.uhden 

 

Sorry for the delay with this query, but I'm still unable to achieve what I need. I tried to try his example, but it doesn't work for me.

 

I have a lisp function called "CD", when I execute the CD function, it asks me for a parameter of the text type, it asks me for an insertion point and it inserts a block.

 

For example:

 

I write CD, it asks me to "Enter the Alias ​​of the symbol", I write the alias of the symbol (which is a text for example "AB") and I do enter, then it shows me the symbol (which is a block), then It prompts me for an insertion point and then when I click or press the enter key anywhere on the plane, the alias block of the symbol I enter is drawn.

 

I have 5 blocks

 

Block 1 - AB
Block 2 - AC
Block 3 - AD
Block 4 - AE
Block 5 - AF

 

I need to call the lisp CD function, draw the 5 blocks, one behind the other block, without having to enter the names of the blocks, but if I could find the insertion point of the blocks and at the end continue with another function lisp.

 

I hope you understand me

 

Thank you very much for reading my message.

 

0 Likes
Message 7 of 35

Sea-Haven
Mentor
Mentor

Post your CD lisp so we can see what your doing otherwise people trying to help are going around in circles.

0 Likes
Message 8 of 35

arperezinf
Advocate
Advocate

Hello @Sea-Haven 

 

Sorry, I am not uploading the CD lisp routine to the forum, because I do not have access to the CD function, so I am explaining what the CD function does, and since I do not have access to the CD function, for that reason I want to make a function lisp, which calls the CD function, so that it loads the 5 blocks at once, requesting an insertion point for each block.

So, do you have any idea how to build something to call the CD function, from another lisp routine and insert the 5 blocks, asking me for the insertion point ?

 

Thank you very much for reading my message.

0 Likes
Message 9 of 35

john.uhden
Mentor
Mentor

Okay, if CD is actually an AutoCAD command then maybe...

(defun c:CDALL ()
  (command "._CD" "AB" ""); Block 1
  (command "._CD" "AC" ""); Block 2
  (command "._CD" "AD" ""); Block 3
  (command "._CD" "AE" ""); Block 4
  (command "._CD" "AF" ""); Block 5
  (princ)
)

But if each CD command is waiting for an insertion point, I don't understand the "".

With cmdecho turned on could you please run CD just once and post what the textscreen shows?

John F. Uhden

0 Likes
Message 10 of 35

arperezinf
Advocate
Advocate

Hello @john.uhden 

 

I tried what you said, but it doesn't work for me, it just gives me the first block "AB", the CD function is not called again.

 

This is what it shows me when I call the lisp CD function.

Point 1: This shows when I write CD.
Point 2: This shows when I hit the enter key.

arperezinf_lisp_function.png

This is all that the CD lisp function shows.

 

Thank you very much for reading my message.

0 Likes
Message 11 of 35

john.uhden
Mentor
Mentor

@arperezinf 

I wasn't clear enough.

1.  Type CMDECHO and make sure it's 1.

2.  Execute the command CD and respond to the prompts.

3.  After the command completes, hit the F2 key and it will bring up your text screen.

4.  Copy and paste what's on that screen into a new response.

Thanks.  We'll figure this out.

John F. Uhden

0 Likes
Message 12 of 35

arperezinf
Advocate
Advocate

Hello @john.uhden 

 

This is what it shows me when I do CMDECHO and run the CD command.

arperezinf_lisp_funcion_command.png

That's all it shows

 

Thank you very much for reading my message.
Greetings.

0 Likes
Message 13 of 35

john.uhden
Mentor
Mentor

I meant to copy the text from the text screen (command history) into your typed response, but I think what you gave us may be good enough.

So the command is looking for just two (2) responses it seems, being...

1.  The name of the block to insert.

2.  The insertion point.

Is that correct?

 

That doesn't make sense if you are using (command "_.CD" "AA" "") because how would it get the insertion point?

Does the command repeat within itself, repeatedly prompting for an insertion point?  What happens if you don't provide an insertion point?  Does it insert the same block at some previous point?

 

And didn't you say you want all 5 blocks to insert at the same location at the first?

John F. Uhden

0 Likes
Message 14 of 35

arperezinf
Advocate
Advocate

Hello @john.uhden 

 

Yes, what you say is correct, the command only looks for two options, just as you say.

 

I do not want the 5 blocks to be inserted all in the same place, I need the CD command to continue working, because that command is the one who controls the blocks, I only need a lisp function that passes the names of the blocks to the CD command, without have to write them and let the CD command do the rest of the process, find me block1, show it, ask me for the insertion point and repeat the process and when those 5 blocks are inserted, stop the CD command.

 

I do not know if this will be possible to do so.

 

I give you a little more information and sorry if I repeat the same thing again, but I am trying not to be confused.

 

The CD command works with many blocks (like a database), each one is called by text (alias symbols), for example "AB", which represents say "Block 1". So, if I don't write the alias of the symbol, it doesn't insert anything and I have to press the escape key to cancel the command.

 

When I use the CD command for the first time, it doesn't know which block I want to insert, so I have to tell it which block I want to insert. Once the CD command is used, it recognizes the last inserted block, if you look at the images you can see where it says "Symbol Aliases", there is a text for example: "<PV>" between the symbols for greater than and less than, that text corresponds to the last inserted block and in the white area where we insert the text, it also puts the same text as above: "PV" of the last inserted block.

 

Therefore, if I use the command once and press the enter key again, I can re-insert the last block for example: "PV", if I press the enter key again repeat the same process and insert the same block " PV ", I can insert the blocks in any part of the plane and until I enter a new alias of the symbol, I will not change the block, so if it was the CD command, the last one I use, I can press the enter key several times and repeat the previous process

 

I hope you understand me up to here.

 

Thank you very much for reading my message.

 

Greetings.

0 Likes
Message 15 of 35

john.uhden
Mentor
Mentor
Okay, I get it.
So, do you want one command that inserts the same 5 blocks, in sequence?
Is it always the same 5 blocks?
And do you want it to let you enter the alias, say "AB" instead of "Block
1?"
If the program can translate the block name from the alias all the time,
then there's nothing extra to do. Otherwise, you'll have to provide a list
of aliases for the real block names. But will the program accept "Block 1"
instead of "AB?"
I mean, what if you enter an alias that is not paired with a real block
name, or what if the real block name doesn't exist in your drawing?

John F. Uhden

0 Likes
Message 16 of 35

arperezinf
Advocate
Advocate

Hello @john.uhden 

 

Yes, they are always the same blocks.


If an alias is entered that does not belong to a block, the CD command gives an error and exits the command.
Aliases are AB, AC, AD, AE, AF.

 

I write the CD command in autocad.
I hit the Enter key.
It asks me to enter the alias of the symbol.
I write AB for example.
I hit the Enter key.

He shows me the block, puts the chrosshair and the rectangles with the coordinates.
I click on any part of the plane and insert the block that corresponds to the symbol of the alias "AB".
End of CD command.

 

I have to do that with the four remaining aliases.

 

If I press the enter key again I get the CD command, with the last alias of the block that I enter "AB", if I press enter, I repeat the process that I previously explained, if I enter a new alias, the command repeats the process that I explained previously but shows me another block.

 

In the following image you can see what the CD command does when I run it for the first time.

 

arperezinf_lisp_funtion.png

 

I started to think and something like this came out ... haha 🤣, but it doesn't work for me.😂😭

 

(defun c: CDALL 
  ()
  ; I declare the aliases of the blocks.
  (setq Block1 "AB")
  (setq Block2 "AC")
  (setq Block3 "AD")
  (setq Block4 "AE")
  (setq Block5 "AF")
  (setq Ctn 0) ; counter starts at zero
  ; start loop of as many repetitions as blocks
  (repeat 5 
    (command "._CD" Block1 "") ; we call the CD command and it pass the alias of the blocks
    (setq Ctn     (+ Ctn 1) ; increment Ctn by 1
          (princ)
    )
  )

 

Thank you very much for reading my message.

Greetings.

0 Likes
Message 17 of 35

john.uhden
Mentor
Mentor
Accepted solution

Okay, I think I've got it...

(defun c:CD5 ()
  (foreach block '("AB" "AC" "AD" "AE" "AF")
    (command "_.CD" block pause)
  )
  (princ)
)

That should invoke the CD command 5 times, being once for each of the 5 blocks, each time pausing for you to pick the insertion point for each one.

John F. Uhden

Message 18 of 35

Kent1Cooper
Consultant
Consultant

If CD does only what you describe, you probably don't need it.  I would modify @john.uhden 's suggestion:

(defun c:CD5 ()
  (foreach block '("AB" "AC" "AD" "AE" "AF")
    (command "_.insert" block pause "" "" "")
  )
  (princ)
)

Remove one of the "" Enters if the Blocks are defined for uniform scaling.

 

Kent Cooper, AIA
Message 19 of 35

john.uhden
Mentor
Mentor
@Kent1Cooper
I think the OP has to use (or is used to using) his CD command primarily
because the aliases to the real block names are built in. F'rinstance "AB"
is an alias for "Block 1" etc. There are actually no blocks named "AB"
etc. In fact the block definitions might not even be in the drawing, but
CD finds them wherever they are. Plus I gather that his CD command takes
care of scaling and rotation.

John F. Uhden

Message 20 of 35

arperezinf
Advocate
Advocate

Hello @Kent1Cooper @john.uhden 

 

If the CD command handles scaling and rotation.

The CD5 lisp function works!!! 😀🙏👍

But I get this message after I insert each block.

 

Unknown command "CD5". Press F1 for help.

 

I tried to rename the function but it does the same. ☹️😭

It also shows this symbol when requesting the insertion point.

arperezinf_lisp_function.png

One detail, you can put a message before inserting each block that says something like this:

Inserting block (Block number) "Block aliases":

Example of the message: Inserting block1 "AB":

 

Thank you very much for reading my message.

0 Likes