LISP Routine - Insert Block by Defining Attribute Values & Custom Parameters

LISP Routine - Insert Block by Defining Attribute Values & Custom Parameters

emreakyazici
Advocate Advocate
2,874 Views
14 Replies
Message 1 of 15

LISP Routine - Insert Block by Defining Attribute Values & Custom Parameters

emreakyazici
Advocate
Advocate

Hello everyone, I have a couple of blocks that I use quite frequently, therefore I want to create a LISP command that inserts block by defining att values and custom parameters (position x1, y1, angle1 etc.) as constants.

 

As you can see in figure1, for example, I want to insert my BLOCK1 with (numbers are just for example, of course)=>

Rotation=0

Scale=1

Base Point= @(0, -10)

Repeat= YES

emreakyazicigsl_0-1626281306951.png

 

Custom:

Angle1=0, Angle2:0,

Position1X= 9, Position1Y : 3

Position2X= 0, Position2Y : 53.5

Position3X=20, Position3Y : 20

Position4X=10, Position4Y : 10

 

Attributes:

TAG10=VALUE10

TAG9=VALUE9

TAG8=VALUE8

TAG7=VALUE7

TAG6=VALUE6

TAG5=VALUE5

TAG4=VALUE4

TAG3=VALUE3

TAG2=VALUE2

TAG1=VALUE1

 

as constants. Of course, after one example, I'll define them for each of my block and case.

 

I want to define these attributes and custom parameters for a single block insertion, not want to change all BLOCK1 attributes and custom parameters. Right now, I'm only use these kinds of simple commands:

 

 

 

(defun c:INS123()
(command "-insert" "BLOCK1" "B" "0, -10" "S" "1" "R" "180" "RE" "Y"))

 

 

 

 

If you can help me, that would be amazing. Thanks for your attention.

0 Likes
Accepted solutions (2)
2,875 Views
14 Replies
Replies (14)
Message 2 of 15

emreakyazici
Advocate
Advocate

any help? 😞

0 Likes
Message 3 of 15

ВeekeeCZ
Consultant
Consultant

A good start would be posting your block1 as dwg.

0 Likes
Message 4 of 15

emreakyazici
Advocate
Advocate
Accepted solution

Oh, sorry. Here you are

0 Likes
Message 5 of 15

ВeekeeCZ
Consultant
Consultant
Accepted solution

Fill in the rest of the values.

Use a spacebar to repeat.

 

(vl-load-com)

(defun c:INS123 ( / o dynprops atvalues)

  (command-s "_.-insert" "BLOCK1" "_b" "0,-10" "_s" 1 "_r" 180 pause)

  (setq dynprops '(
		   ("Position4 X" . 10)			; all dyn-values are numbers - without ""
		   ("Position4 Y" . 10)
		   )
	
	atvalues '(
		   ("TAG4" . "10")                  	; case-sensitive !!
		   ("TAG7" . "SIL-BENI-new")		; all atts are "strings" - with ""
		   ))
	

  (setq o (vlax-ename->vla-object (entlast)))
  (LM:setdynprops o dynprops)
  (LM:vl-setattributevalues o atvalues)
  (princ)
)

;; Set Dynamic Block Properties  -  Lee Mac
;; Modifies values of Dynamic Block properties using a supplied association list.
;; blk - [vla] VLA Dynamic Block Reference object
;; lst - [lst] Association list of (( . ) ... )
;; Returns: nil

(defun LM:setdynprops ( blk lst / itm )
    (setq lst (mapcar '(lambda ( x ) (cons (strcase (car x)) (cdr x))) lst))
    (foreach x (vlax-invoke blk 'getdynamicblockproperties)
        (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
            (vla-put-value x (vlax-make-variant (cdr itm) (vlax-variant-type (vla-get-value x)))))))


;; Set Attribute Values  -  Lee Mac
;; Sets attributes with tags found in the association list to their associated values.
;; blk - [vla] VLA Block Reference Object
;; lst - [lst] Association list of (( . ) ... )
;; Returns: nil

(defun LM:vl-setattributevalues ( blk lst / itm )
    (foreach att (vlax-invoke blk 'getattributes)
        (if (setq itm (assoc (vla-get-tagstring att) lst))
            (vla-put-textstring att (cdr itm)))))

 

0 Likes
Message 6 of 15

Sea-Haven
Mentor
Mentor

A couple of comments you can get tagnames of the block, like wise you can get dynamic properties name, so can pop a DCL straight after inserting block giving you the oppurtunity to enter values. This is a global type answer for any block.

 

Or can ask 1st then insert and update. I use multi getvals.lsp. You can set default values, I have used this to set visibilty states. 

 

Happy to provide more info.

SeaHaven_0-1626486012290.png

 

 

 

 

 

0 Likes
Message 7 of 15

emreakyazici
Advocate
Advocate

Oh thank you. It's almost perfect, but I had a problem on dynprops, I should set dynprop angle but I'm only able to define as radiants. I need to do it in degrees.

 

And my second problem is, my position4 controls, position3 and position2. Therefore, I tried to define Pos4 first, then pos2 and pos3. Normally I do that and get normal results, pos2 and pos3 does not affect pos4 but pos4 affects pos3 and pos2. But it was not the expected result (code1 vs. figure1.)

emreakyazicigsl_0-1626542161591.png

emreakyazicigsl_1-1626542314630.png

 

 

 

;Code1:
		   ("Position4 X" . 30)			
		   ("Position4 Y" . 30)

		   ("Position2 X" . 1)			
		   ("Position2 Y" . 2)

		   ("Position3 X" . 3)			
		   ("Position3 Y" . 4)

 

0 Likes
Message 8 of 15

emreakyazici
Advocate
Advocate

My aim is defining all the dyns and atts before each block that I'm planning to use, and assign them to shortcuts and put them with prepared atts precisely. Therefore, an input process for each block would decrease my efficiency. Thank you.

0 Likes
Message 9 of 15

Sea-Haven
Mentor
Mentor

RTD and DTR very old radian <--> degrees defuns every person who does lisp should know about these. Hence did not post find them.

0 Likes
Message 10 of 15

emreakyazici
Advocate
Advocate

I tried but couldn't find anything that solves my problem. I tried to setq Ang1 ((5* pi (/  180.0) and redefine the line as ("Angle1" . Ang1) but that didn't help. On the other hand, if you don't classify the other question as easy as everybody knows, can you help me about that? Since the angle is last custom dynprop, change of angle ruins my other position inputs. How can I solve that? Thank you.

emreakyazicigsl_0-1626639046965.png

 

0 Likes
Message 11 of 15

Sea-Haven
Mentor
Mentor

The two defuns

 

;The dtr function converts degrees to radians
;The rtd function converts radians to degrees
(defun dtr (a)
(* pi (/ a 180.0))
)
;
(defun rtd (a)
(/ (* a 180.0) pi)
)

 

 

Thats a typo  setq Ang1 ((5* pi (/  180.0)  see dtr above (setq ang1 (dtr ang))

 

 

 

0 Likes
Message 12 of 15

emreakyazici
Advocate
Advocate

Thanks for your help but I still get the error below from command bar.

 

(defun c:41 ( / o dynprops atvalues)

(defun dtr (a)
(* pi (/ a 180.0))
)

(setq ang1 (dtr 20.0))


  (command-s "_.-insert" "BLOCKNAME1" "_b" "0,-10" "_s" 1 "_r" 0)
  (setq dynprops '(

		   ("Angle1" . ang1)
...
...
; error: lisp value has no coercion to VARIANT with this type:  ANG1

 

 

0 Likes
Message 13 of 15

ВeekeeCZ
Consultant
Consultant

Dear @emreakyazici 

just to be clear. Your block is such a mess that I can't even tell how it suppose to work. Sorry, I am not going to spend hours investigating whether the code is bad (probably not) or your block definition (very likely).

 
0 Likes
Message 14 of 15

emreakyazici
Advocate
Advocate

I redefined and changed the layers, name of blocks/tags/values in order to not break confidentiality, didn't spent lots of time. Besides this angle situation and order of props, your code is excellent and I'm grateful to you. If I annoyed you, I'm sorry.

0 Likes
Message 15 of 15

Sea-Haven
Mentor
Mentor

The error is not in DTR but your put dynamic properties, look at Lee-mac dynamic properties.lsp

(LM:setdynpropvalue (vlax-ename->vla-object  (entlast)) "DoorWidth"  x)
(LM:setdynpropvalue (vlax-ename->vla-object  (entlast)) "DoorHeight"  y)

 

0 Likes