Code doesn't work - AutoLISP

Code doesn't work - AutoLISP

vinju
Explorer Explorer
527 Views
5 Replies
Message 1 of 6

Code doesn't work - AutoLISP

vinju
Explorer
Explorer

Hello everyone,

 

I've been trying to write a script that accomplishes the following:

 

- Let me select a viewport in a layout

- Get the rotation angle of the UCS in relation to the WCS using VIEWTWIST

- Select a specific dynamic block that has a parameter called "Winkel1" (German for: Angle1)

- Apply the number that I got by running VIEWTWIST to that parameter.

 

Here's the code:

 

(defun c:autonord()
(setq vp (car (entsel "Select the Viewport: ")))
(setvar "viewport" vp)
(setq angle (getvar "VIEWTWIST"))
(if (< angle 0) (setq angle (+ angle 360)))
(setq blk (car (entsel "Select the dynamic block: ")))
(if (member "Winkel1" (vl-block-dyn-propnames blk))
(command "._ddp" blk "Winkel1" angle "")
)
(princ "Angle value is: " angle)
(princ)
)

And this is the error I've been getting when I run the script and select a viewport:

Error: Settings for AutoCAD variable rejected: "viewport" <objektname: 23b0decadd0>

 

It would be awesome if any of you could help me with this problem!

 

I am using ACAD 2022.

 

Thanks so much in advance!

Accepted solutions (1)
528 Views
5 Replies
Replies (5)
Message 2 of 6

ВeekeeCZ
Consultant
Consultant
Accepted solution

Try and see. If it fails and you to fix it, post a test dwg.

 

(vl-load-com)

(defun c:autonord ( / vp ang blk)
  
  (princ "\nSelect source viewport: ")
  (setq vp (ssget "_+.:E:S" '((0 . "VIEWPORT"))))
  (command "_.mspace")
  (setvar 'cvport (cdr (assoc 69 (entget (ssname vp 0)))))
  (setq ang (getvar "VIEWTWIST"))
  (command "_.pspace")
  (setq blk (car (entsel "Select the dynamic block: ")))
  (setpropertyvalue blk "AcDbDynBlockPropertyWinkel1" ang)
  (princ (strcat "Angle value is: " (angtos ang)))
  (princ)
  )

 

0 Likes
Message 3 of 6

ВeekeeCZ
Consultant
Consultant

@vinju wrote:

Hello everyone,

 

I've been trying to write a script that accomplishes the following:

 

- Let me select a viewport in a layout

- Get the rotation angle of the UCS in relation to the WCS using VIEWTWIST

- Select a specific dynamic block that has a parameter called "Winkel1" (German for: Angle1)

- Apply the number that I got by running VIEWTWIST to that parameter.

 

Here's the code:

 

(defun c:autonord()
(setq vp (car (entsel "Select the Viewport: "))) ; this is tricky, could be used only when the VP is the  "rectangle" type. 
(setvar "viewport" vp) ; there is no system variable of that name
(setq angle (getvar "VIEWTWIST")) ; you'll get that only when the particular VP is active!
(if (< angle 0) (setq angle (+ angle 360))) ; all the system angles are in radians.
(setq blk (car (entsel "Select the dynamic block: ")))
(if (member "Winkel1" (vl-block-dyn-propnames blk))  ; possibly some custom function... you did not share its definition
(command "._ddp" blk "Winkel1" angle "") ;; again, some custom command. don't know it.
)
(princ "Angle value is: " angle)  ; angle is an actual function name, you cannot use this name as a variable! also , princ does not work like that. Princ print only 1 string. you have 2 items and angle is not a string.
(princ)
)

And this is the error I've been getting when I run the script and select a viewport:

Error: Settings for AutoCAD variable rejected: "viewport" <objektname: 23b0decadd0>

 

It would be awesome if any of you could help me with this problem!

 

I am using ACAD 2022.

 

Thanks so much in advance!


 

0 Likes
Message 4 of 6

vinju
Explorer
Explorer

Hey BeekeeCZ,

 

thanks for taking the time to help me.

 

Sadly, the code still didn't work. But it plotted a error-message I haven't encountered yet:

 

Error: Incorrect argument type: FILE "344.83"

 

I have added a test drawing to this response. The dynamic block is the one with the north arrow.

 

Maybe you can give it another try 🙂

 

Thanks so much!

0 Likes
Message 5 of 6

ВeekeeCZ
Consultant
Consultant

Ok, updated.

0 Likes
Message 6 of 6

vinju
Explorer
Explorer

Awesome! It works! - Thank you so much 😄

0 Likes