Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lisp, script to insert block setting just the insertion point

19 REPLIES 19
SOLVED
Reply
Message 1 of 20
GeryKnee
2796 Views, 19 Replies

Lisp, script to insert block setting just the insertion point

 

Hello there,

 i need a Lisp script to insert block that waits just the insertion point definition from user and always sets scale to 1 and rotation to 0

Can anybody help me in this?

Thanx

Gery

19 REPLIES 19
Message 2 of 20
hmsilva
in reply to: GeryKnee

As a demo

 

(defun c:demo ( / dwg pt)
  (if (and (setq dwg (findfile "c:/MyPath/MyBlk.dwg"))
           (setq pt (getpoint "\nSpecify insertion point: "))
           )
    (command "_.insert" dwg "_S" 1 "_R" 0 "_NONE" pt)
    )
  (princ)
  )

 

Hope that helps

Henrique

EESignature

Message 3 of 20
GeryKnee
in reply to: hmsilva

Thanx Henrique

It's about what i need.

 

i turned code to


(defun c:demo ( MyBlk / pt)
  (if (setq pt (getpoint "\nSpecify insertion point: "))
    (command "_.insert" MyBlk "_S" 1 "_R" 0 "_NONE" pt)
   )
  (princ)
  )

 

Thats because i want to call it from command line typing the block name (wich is a dwg file located in my supported paths) :::

> demo BlkName

Something is wrong in above scripts , that doesn't work

Message 4 of 20
hmsilva
in reply to: GeryKnee

Try

 

(c:demo "Blockname")

 

Henrique

EESignature

Message 5 of 20
Kent1Cooper
in reply to: GeryKnee

You can't use arguments in a C:-defined command.  Do it this way:

 

(defun demo ( MyBlk / pt) ; without the C:

....

 

Usage:

 

(demo "YourBlockName")

 

But I would do it a little differently, so that you can see it dragging into place as you go for the insertion point.  The command will provide its own prompt, and the routine can end and just leave the command at that point:

 

(defun demo (MyBlk)
  (command "_.insert" MyBlk "_S" 1 "_R" 0)
)

Kent Cooper, AIA
Message 6 of 20
hmsilva
in reply to: Kent1Cooper


@Kent1Cooper wrote:

You can't use arguments in a C:-defined command.  Do it this way:

 

(defun demo ( MyBlk / pt) ; without the C:

....

 

Usage:

 

(demo "YourBlockName")

 

But I would do it a little differently, so that you can see it dragging into place as you go for the insertion point.  The command will provide its own prompt, and the routine can end and just leave the command at that point:

 

(defun demo (MyBlk)
  (command "_.insert" MyBlk "_S" 1 "_R" 0)
)


Are you sure.

We should not use arguments in a c: defined command, because it can be interpreted as a command line 'command', but is a function name and any other.

But you are correct the function named only demo is far more correct.

 

Cheers

Henrique

 

EESignature

Message 7 of 20
GeryKnee
in reply to: hmsilva

 

 

I wrote the following because i want the block to be exploded

 


(defun c:Demo () ;
   (setq cmde (getvar "cmdecho"))
   (setvar "cmdecho" 0)
   (if (and (setq BlockName (getString "\nSpecify block name: "))
            (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
            )
       (command "_.insert" BlockName "_S" 1 "_R" 0 "_NONE" InsertionPoint)
       (while (> (getvar 'cmdactive) 0)
              (command pause)
       )
       (command "explode" "last")
   )
   (setvar "cmdecho" cmde)
   (princ)
   )

 

The above does't work why?

 

The following works instead but doesn't explode

 


(defun c:InsArchBlocks () ;
   (setq cmde (getvar "cmdecho"))
   (setvar "cmdecho" 0)
   (if (and (setq BlockName (getString "\nSpecify block name: "))
            (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
            )
       (command "_.insert" BlockName "_S" 1 "_R" 0 "_NONE" InsertionPoint)
   )
   (setvar "cmdecho" cmde)
   (princ)
   )

 

Message 8 of 20
stevor
in reply to: GeryKnee

While simply asking the question is easy,

it is not always the quickest way to a usable answer.

Before you ask, if you search some,

you may find  a usable solution,

or find better words for the question.

IE, google: +insert +block autolisp

will find many examples.

 

What you probably want

is to use the 'getfiled    function

to find a file to insert.

 

So then google: +insert +block +getfiled    autolisp

 

S
Message 9 of 20
marko_ribar
in reply to: GeryKnee


@GeryKnee wrote:

 

 

I wrote the following because i want the block to be exploded

 


(defun c:Demo () ;
   (setq cmde (getvar "cmdecho"))
   (setvar "cmdecho" 0)
   (if (and (setq BlockName (getString "\nSpecify block name: "))
            (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
            )
       (command "_.insert" BlockName "_S" 1 "_R" 0 "_NONE" InsertionPoint)
       (while (> (getvar 'cmdactive) 0)
              (command pause)
       )
       (command "explode" "last")
   )
   (setvar "cmdecho" cmde)
   (princ)
   )

 

The above does't work why?

 

The following works instead but doesn't explode

 


(defun c:InsArchBlocks () ;
   (setq cmde (getvar "cmdecho"))
   (setvar "cmdecho" 0)
   (if (and (setq BlockName (getString "\nSpecify block name: "))
            (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
            )
       (command "_.insert" BlockName "_S" 1 "_R" 0 "_NONE" InsertionPoint)
   )
   (setvar "cmdecho" cmde)
   (princ)
   )

 


To see where you made mistake in your (c:Demo) function, refer to the following link...

 

Recently, I've made the same mistake...

 

http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/finding-files-within-paths/m-p/532284...

 

M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 10 of 20
Kent1Cooper
in reply to: hmsilva


@hmsilva wrote:

@Kent1Cooper wrote:

You can't use arguments in a C:-defined command.  Do it this way:

....


 

Are you sure.

....


From the AutoLisp Developer's Guide, under C:XXX functions:

 

To use functions as AutoCAD commands, be sure they adhere to the following rules:

  • The function name must use the form C:XXX (upper- or lowercase characters). The C: portion of the name must always be present; the XXX portion is a command name of your choice. C:XXX functions can be used to override built-in AutoCAD commands. (See Redefining AutoCAD Commands.)
  • The function must be defined with no arguments.

Admittedly, that's from back in the 2004 edition.  Oddly, Help in 2015 is milder about it:

 

Functions that are defined as commands should not accept arguments directly, instead input for a command should be obtained using one of the getXXX functions.

 

 

Why they use "should" rather than "must" as before, I couldn't say -- it's hard to imagine that the operation of (defun) has changed.

Kent Cooper, AIA
Message 11 of 20
Kent1Cooper
in reply to: GeryKnee


@GeryKnee wrote:

 

I wrote the following because i want the block to be exploded

 

....

 

The following works instead but doesn't explode


(defun c:InsArchBlocks () ;
   (setq cmde (getvar "cmdecho"))
   (setvar "cmdecho" 0)
   (if (and (setq BlockName (getString "\nSpecify block name: "))
            (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
            )
       (command "_.insert" BlockName "_S" 1 "_R" 0 "_NONE" InsertionPoint)
   )
   (setvar "cmdecho" cmde)
   (princ)
   )


[In the first one (not quoted above), It's not about finding the file, but the missing (progn) wrapper around the 'then' argument to the (if) function.]

 

But you can make that second one [quoteed above] work by Inserting the Block already exploded, with an asterisk prefix:

 

(defun c:InsArchBlocks () ;
   (setq cmde (getvar "cmdecho"))
   (setvar "cmdecho" 0)
   (if (and (setq BlockName (getString "\nSpecify block name: "))
            (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
            )
       (command "_.insert" (strcat "*" BlockName) "_S" 1 "_R" 0 "_NONE" InsertionPoint)
   )
   (setvar "cmdecho" cmde)
   (princ)
 )

 

That does, however, preclude the option of dragging it visibly into place as I suggested before, because dynamic positioning like that doesn't work when inserting something pre-exploded.

Kent Cooper, AIA
Message 12 of 20
hmsilva
in reply to: Kent1Cooper


@Kent1Cooper wrote:

@hmsilva wrote:

@Kent1Cooper wrote:

You can't use arguments in a C:-defined command.  Do it this way:

....


 

Are you sure.

....


From the AutoLisp Developer's Guide, under C:XXX functions:

 

...

 

Functions that are defined as commands should not accept arguments directly, instead input for a command should be obtained using one of the getXXX functions.

 

 

Why they use "should" rather than "must" as before, I couldn't say -- it's hard to imagine that the operation of (defun) has changed.


Did you try to call the function with (c:demo "BlockName")?
Henrique

EESignature

Message 13 of 20
hmsilva
in reply to: Kent1Cooper


@Kent1Cooper wrote:

...

But you can make that second one [quoteed above] work by Inserting the Block already exploded, with an asterisk prefix:

(defun c:InsArchBlocks () ;
   (setq cmde (getvar "cmdecho"))
   (setvar "cmdecho" 0)
   (if (and (setq BlockName (getString "\nSpecify block name: "))
            (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
            )
       (command "_.insert" (strcat "*" BlockName) "_S" 1 "_R" 0 "_NONE" InsertionPoint)
   )
   (setvar "cmdecho" cmde)
   (princ)
 )

...


That was my first thought, but it throws an error, changing the prompt order, forcing the scale and rotation it errors, is expecting a point, and don't allow to change the prompt order...

Only with:

 

(defun demo (BlkName / pt)
  (if
    (setq pt (getpoint "\nSpecify insertion point: "))
     (command "_.-insert" (strcat "*" BlkName) "_NONE" pt "" "")
  )
  (princ)
)

 

Weird...

 

Henrique

EESignature

Message 14 of 20
GeryKnee
in reply to: Kent1Cooper

 

Sorry , but my lisp knowledge is not enough and my english not so good

 

I need to build a command that uses a BlockName and and an Insertion point *just them) witch will be inserting by user , that inserts the block to the Insertion point and after that explodes the inserted block.

i do;t know what i have to write.

the followng inserts it BUT DOESN'T EXPLODE IT


(defun c:InsArchBlocks () ;
   (setq cmde (getvar "cmdecho"))
   (setvar "cmdecho" 0)
   (if (and (setq BlockName (getString "\nSpecify block name: "))
            (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
            )
       (command "_.insert" BlockName "_S" 1 "_R" 0 "_NONE" InsertionPoint)
   )
   (setvar "cmdecho" cmde)
   (princ)
   )

 

So, what else sould be written ?

 

Thanx

Gery

Message 15 of 20
hmsilva
in reply to: GeryKnee

Gery try

 

(defun c:InsArchBlocks (/ BlockName cmde InsertionPoint)
  (setq cmde (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (if (and (setq BlockName (getString "\nSpecify block name: "))
           (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
      )
    (command "_.insert" (strcat "*" BlockName) "_NONE" InsertionPoint "" "")
  )
  (setvar "cmdecho" cmde)
  (princ)
)

;; or

(defun InsArchBlocks (BlockName / cmde InsertionPoint)
  (setq cmde (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (if (setq InsertionPoint (getpoint "\nSpecify insertion point: "))
    (command "_.insert" (strcat "*" BlockName) "_NONE" InsertionPoint "" "")
  )
  (setvar "cmdecho" cmde)
  (princ)
)
;; usage
;; (InsArchBlocks "YourBlockName")

 

 

Henrique

EESignature

Message 16 of 20
GeryKnee
in reply to: hmsilva

 

No, that doesn't work

 

The following works :


(defun fInsertOnInsP (BlkName / pt)
  (if
    (setq pt (getpoint "\nSpecify insertion point: "))
     (command "_.-insert" (strcat "*" BlkName) "_NONE" pt "" "")
  )
  (princ)
)

(defun c:InsArchBlocks1 ()
   (if (setq BlockName (getString "\nSpecify block name: "))
       (fInsertOnInsP BlockName )
   )
   (princ)
   )
)

 

Because i need to set layer "1" (wich exists in my drawing) to Frzen state i wrote as

 


(defun fInsertOnInsP (BlkName / pt)
  (if
    (setq pt (getpoint "\nSpecify insertion point: "))
     (command "_.-insert" (strcat "*" BlkName) "_NONE" pt "" "")
  )
  (princ)
)

(defun c:InsArchBlocks1 ()
   (if (setq BlockName (getString "\nSpecify block name: "))
       (fInsertOnInsP BlockName )
       (command "_.layer" "F" "1" "")
   )
   (princ)
   )
)

 

But that's not setting layer "1" too Frozen

 

What can i do ?

 

Message 17 of 20
hmsilva
in reply to: GeryKnee

Gery,

 

the if function evaluates the test expression, if not nil will evaluate the next expression, if nil will evaluate the else expression...

 

;; your code
(if (setq BlockName (getString "\nSpecify block name: "));; test exp
       (fInsertOnInsP BlockName );; if not nil exp
       (command "_.layer" "F" "1" "");; if nil exp
   )

;; change to
(if (setq BlockName (getString "\nSpecify block name: "));; test exp
  (progn;; if not nil exp
    (fInsertOnInsP BlockName)
    (command "_.layer" "F" "1" "")
  );; if not nil
);; no if nil exp

 

Henrique

EESignature

Message 18 of 20
GeryKnee
in reply to: hmsilva

 

 

Thanks Henrique

Everything solved

Regards,

Gery

Message 19 of 20
hmsilva
in reply to: GeryKnee

You're welcome, Gery
Glad I could help

Henrique

EESignature

Message 20 of 20
Kent1Cooper
in reply to: hmsilva


@hmsilva wrote:
.... Did you try to call the function with (c:demo "BlockName")?

No, I hadn't -- I just believed what I cited from the AutoLISP Developer's Guide, a "fact" that I have also seen others cite around here, and which has always seemed to be "the way it is."  But sure enough, a C:XXX comand definition can be done with arguments.  It's interesting that there would be an outright untruth in the documentation like that.  [But I can also imagine that there might be circumstances I haven't encountered in which it would cause trouble, which would be a reason for the prohibition, though I don't have any ideas as to what they might be.]

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost