Command not executing with AutoLISP (newbie)

Command not executing with AutoLISP (newbie)

m.grieseRNJX2
Explorer Explorer
762 Views
7 Replies
Message 1 of 8

Command not executing with AutoLISP (newbie)

m.grieseRNJX2
Explorer
Explorer

Hi guys,

 

I want to mention upfront that I am a newbie to AutoCAD and AutoLISP. I am trying to help a colleague to automate certain processes. I am using AUtoCAD in German so the commands are in German but I added a translation comment for each command type.

In the code snippet, we need to select a poly line. Then I need to create rectangle orthogonal to the poly line to use it for a sweep command later on (not in the snippet). To get the orthogonal rectangle, I create a new User Coordinate System (UCS) with the XY plane orthogonal to the poly line. Then create the rectangle on the XY plane and switch back to the original UCS.

Unfortunately, the code is not working. In detail, the command where I create my new UCS and switch back to the old one, as well as when creating the rectangle. I debugged my code and when I apply 

(entget (entlast)) after those commands, I get the previously created point.
I'd appreciate if you help me finding out what is wrong and how I can detect those errors in the future. Thank you!
 

 

(defun C:POLY_TEST (/)
  (setq poly (car (entsel "\nSelect Polyline: ")))
  (setq lst (3d-coord->pt-lst (vlax-get (vlax-ename->vla-object poly) 'Coordinates)))
  (setq 
    firstPoint (nth 0 lst);First Point of Poly line
    x (nth 0 firstPoint)
    y (nth 1 firstPoint)
    z (nth 2 firstPoint)
    secondPoint (nth 1 lst);Second point of poly line
    x2 (nth 0 secondPoint)
    y2 (nth 1 secondPoint)
    z2 (nth 2 secondPoint)
    lastPoint (nth (- (length lst) 1) lst)
    xEnd (nth 0 lastPoint)
    yEnd (nth 1 lastPoint)
    zEnd (nth 2 lastPoint)
    vXYx (- x2 x); direction vector between first and second point of poly line
    vXYy (- y2 y)
    vXYz (- z2 z)
    uXYx 0;direction vector pointing in negative z
    uXYy 0
    uXYz -1
    rXYx (- (* vXYy uXYz) (* vXYz uXYy)); cross product to get orthogonal vector between two previous vectors
    rXYy (- (* vXYz uXYx) (* vXYx uXYz))
    rXYz (- (* vXYx uXYy) (* vXYy uXYx))
    rLen (sqrt (+ (* rXYx rXYx) (* rXYy rXYy) (* rXYz rXYz))); vector length
    rXYx (/ rXYx rLen); normalizing vector
    rXYy (/ rXYy rLen)
    rXYz (/ rXYz rLen)
    pXYx (+ x uXYx rXYx);calculate point on plane orthogonal to vector between first and second point of poly line
    pXYy (+ y uXYy rXYy)
    pXYz (+ z uXYz rXYz)
  )
  (command "PUNKT" (strcat (rtos x 2 11) "," (rtos y 2 11) "," (rtos z 2 11)) ""); PUNKT  = POINT
  (setq origin (entget (entlast)))
  (command "PUNKT" (strcat (rtos x 2 11) "," (rtos y 2 11) "," (rtos (- z 1) 2 11)) "")
  (setq negX (entget (entlast)))
  (command "PUNKT" (strcat (rtos pXYx 2 11) "," (rtos pXYy 2 11) "," (rtos pXYz 2 11)) "")
  (setq pXY (entget (entlast)))
  (command "BKS" origin negX pXY ); BKS = UCS - NOT WORKING!! -> generate new User coordinate system with origin at first point of poly line which XY plane is orthogonal to poly line
  (setq bksNew (entget (entlast)))
  (setq diameter 10.0)
  (command "PUNKT" (strcat (rtos (/ (+ diameter 2) -2) 2 11) "," (rtos (/ (+ diameter 2) -2) 2 11) "," (rtos 0 2 11)) "")
  (setq bottomLeft (entget (entlast))); bottom left point of rectangle
  (command "PUNKT" (strcat (rtos (/ (+ diameter 2) 2) 2 11) "," (rtos (/ (+ diameter 2) 2) 2 11) "," (rtos 0 2 11)) "")
  (setq topRight (entget (entlast))); top right point of rectangle
  (command "RECHTECK" bottomLeft topRight); RECHTECK = RECTANGLE - NOT WORKING!! -> create rectangle orthogonal to poly line
  (setq rect (entget (entlast)))
  (command "BKS" "VO");BKS = UCS; VO = Vorher (Before/Previous) - NOT WORKING!! -> moving back to original UCS
  (setq bksOld (entget (entlast)))
  (command "LÖSCHEN" origin negX pXY bottomLeft topRight ""); LÖSCHEN = DELETE / REMOVE -> Deleting support points for UCS and rectangle
  (princ)
)

 

0 Likes
Accepted solutions (1)
763 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant

What is stored in your negX, pXY, origin, bottomLeft and topRight variables are entity data lists, not point locations.  They contain the point location, but you need to extract it.  Try:

 

....
(command "BKS" (cdr (assoc 10 origin)) (cdr (assoc 10 negX)) (cdr (assoc 10 pXY)))
....
(command "RECHTECK" (cdr (assoc 10 bottomLeft)) (cdr (assoc 10 topRight)))
....

 

Or, if you don't use other things from those entity data lists, set the variables to be the points in the first place, instead of the entity data lists, and then you can use the variables as you have them, for example:

(setq negX (cdr (assoc 10 (entget (entlast)))))

And be sure you have running Object Snap turned off [that can be built into the code if you want].

 

If that works, we can look at simplifying various things, for example:  Do you really need to draw Point objects at those locations and then extract the locations from the objects?  You should be able to just use the locations without the Points.  And it should be possible to do all of this without pulling apart each location into separate X Y & Z coordinates.

Kent Cooper, AIA
0 Likes
Message 3 of 8

Sea-Haven
Mentor
Mentor

Just me if say want rectang along pline sweep path, I draw in UCS world rectang at end point, then use rotate3d to flip the rectang say vertical. Just use ucs ob when rotating. Hope it helps.

SeaHaven_0-1654737356364.png

 

0 Likes
Message 4 of 8

m.grieseRNJX2
Explorer
Explorer

Thank you for your help. This helped me a lot already.

I tried to update my code to make it more efficient as you and Sea-Haven suggested. However, I still struggle with my code when I try to rotate the rectangle. The command for rotate is not accepted. I'd appreciate, if you could check it.

I try to rotate my rectangle 2 time. Once around the X axis and once around the Z axis. I tried to do it with two approaches (one is commented out). In the first approach I specify the axis around which it should be rotated by entering "X" or "Z". But there seems to be something wrong with the command. In the second approach I provide 2 points to specify the axis but it still does not work.

Thank you for the help.

 

 

(defun C:MY_RECTANGLE (/)
  (setq poly (car (entsel "\nSelect FH Polyline: ")))
  (setq lst (3d-coord->pt-lst (vlax-get (vlax-ename->vla-object poly) 'Coordinates)))
  (setq osmodeValue (getvar "osmode"))
  (setvar "osmode" 0)
  (setq
    mp (nth 0 lst);First Point of Poly line = mid point of rectangle
    mpX (nth 0 mp)
    mpY (nth 1 mp)
    mpZ (nth 2 mp)
    radius 10
    p1X (- mpX radius)
    p1Y (- mpY radius)
    p1Z mpZ
    p2X (+ mpX radius)
    p2Y (+ mpY radius)
    p2Z mpZ
    p1 (list p1X p1Y p1Z)
    p2 (list p2X p2Y p2Z)
    sp (nth 1 lst)
    spX (nth 0 sp)
    spY (nth 1 sp)
    angleZ (atan (/ (- spY mpY) (- spX mpX)))
    angleZDeg (+ 90 (/ (* angleZ 180) 3.1412)); angle to rotate around z axis
    xAxisP (list (+ mpX 1) mpY mpZ);second point on x axis
    zAxisP (list mpX mpY (+ mpZ 1));second point on z axis
  )
  (command "RECHTECK" p1 p2)
  (setq rectangle (entget (entlast)))
  ;(command "3DDREHEN" rectangle "X" mp "90") or (command "3DDREHEN" rectangle "" "X" mp "90"); NOT WORKING!
  (command "3DDREHEN" rectangle mp xAxisP "90");3DDREHEN=ROTATE3D NOT WORKING!
  ;(command "3DDREHEN" rectangle "Z" mp angleZDeg) or (command "3DDREHEN" rectangle "" "Z" mp angleZDeg); NOT WORKING!
  (command "3DDREHEN" rectangle mp zAxisP angleZDeg);3DDREHEN=ROTATE3D NOT WORKING!
  (setvar "osmode" osmodeValue)
   (princ)
)

 

0 Likes
Message 5 of 8

Kent1Cooper
Consultant
Consultant

@m.grieseRNJX2 wrote:

....

....
  (command "3DDREHEN" rectangle mp xAxisP "90");3DDREHEN=ROTATE3D NOT WORKING!
....

One thing I notice:  Most editing commands can operate on more than one object at a time, so you need to tell them when you are finished selecting objects:

....
  (command "3DDREHEN" rectangle "" mp xAxisP "90");3DDREHEN=ROTATE3D NOT WORKING!
....

Kent Cooper, AIA
0 Likes
Message 6 of 8

m.grieseRNJX2
Explorer
Explorer

Thank you for the quick response.

Unfortunately, that did not help. When I run this I get the output in the attached image.

The rectangle itself is generated but it is not rotated.

0 Likes
Message 7 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

I hadn't noticed before that your 'rectangle' variable holds an entity data list.  The way it is used later, in editing-command object selection, it looks like you want that to be just the entity name, not its data list.  Try setting it that way:

 

(setq rectangle (entlast))

 

Kent Cooper, AIA
0 Likes
Message 8 of 8

m.grieseRNJX2
Explorer
Explorer
Yeah that was it. Thank you a lot for your help.
0 Likes