Please help! Lisp problem

jlee107
Contributor

Please help! Lisp problem

jlee107
Contributor
Contributor

Hello, just to let you all know I only have basic knowledge of coding and I used ChatGpt to create this code. I'm trying to create a Lisp for zwcad(autocad) that could help me to increase my performance at my job. I want a block name to be automatically chosen and placed according to a length of a polyline selected on a model space after command is executed. So after I select a polyline, it should calculate a length of a polyline and find a midpoint. I want base point of a block to be an inserting point of a block and base point should be placed at midpoint of a polyline. For example if a length of a polyline I select is '8000mm', it calls a block name '7100e (8000-900 + suffix 'e')' from the block list and places that block at midpoint of selected polyline. I am certain there are block names ranging from 5400e~12200e (in increment of 100), but it doesn't seem to be able to find a block name appropriate to the length of a polyline. I've been keep getting this error message "Error: Retrieving the ID of the failed name - nil". Please help me it's urgent...

 

(setq selectedPolyline (car (entsel "\nSelect a polyline: ")))

(if (and selectedPolyline
         (member (cdr (assoc 0 (entget selectedPolyline))) '("POLYLINE" "LWPOLYLINE")))
    (progn
      (setq polylinePoints (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget selectedPolyline))))

      ;; Ensure there are at least two points
      (if (< (length polylinePoints) 2)
          (princ "\nError: The selected polyline must have at least two points.")
        (progn
          (setq startPoint (car polylinePoints))
          (setq endPoint (cadr polylinePoints))

          ;; Calculate the midpoint
          (setq midpoint
                (list (/ (+ (car startPoint) (car endPoint)) 2.0)
                      (/ (+ (cadr startPoint) (cadr endPoint)) 2.0)
                      0.0))

          ;; Calculate polyline length
          (setq polylineLength
                (distance (list (car startPoint) (cadr startPoint) 0.0)
                          (list (car endPoint) (cadr endPoint) 0.0)))

          ;; Calculate block name
          (setq blockName (strcat (rtos (- polylineLength 900) 2 2) "e"))

          ;; Insert block at the calculated midpoint
          (command "_.INSERT" blockName midpoint 1.0 1.0 0.0)

          (princ (strcat "\nBlock '" blockName "' placed."))
        )
      )
    )
  (princ "\nError: Please select a valid polyline.")
)

 

0 Likes
Reply
314 Views
6 Replies
Replies (6)

cadffm
Consultant
Consultant

Hi,

 

there is a special forum for Lisp, next door.

 

 

>  "I only have basic knowledge of coding"

Okay

> "and I used ChatGpt to create this code."

😞

 

I will not give my full support to beginners-with-chatGPTcode (not good, if you don't know the basics), but:

 

 

 Look at RTOS

 (rtos (- polylineLength 9002 2)

Polylineline length = 1922.00000000

result = "1022.00"

 

You need zero decimal numbers

 (setq blockName (strcat (rtos (- polylineLength 9002 0"e"))

Sebastian

0 Likes

cadffm
Consultant
Consultant

Hi again,

 

 

back at my PC I checked the code: It will work! (with my tiny 'rtos' edit)

 

some comments more

 

 

(defun c:TEST nil

(setq selectedPolyline (car (entsel "\nSelect a polyline: ")))

(if (and selectedPolyline
         (member (cdr (assoc 0 (entget selectedPolyline))) '("POLYLINE" "LWPOLYLINE"))) ; Allow 2 kind of plines
     (progn
      (setq polylinePoints (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget selectedPolyline)))) ; will NOT work with POLYLINE objects"

      ;; Ensure there are at least two points
      (if (< (length polylinePoints) 2)
          (princ "\nError: The selected polyline must have at least two points.")
        (progn
          (setq startPoint (car polylinePoints))
          (setq endPoint (cadr polylinePoints))

          ;; Calculate the midpoint
          (setq midpoint
                (list (/ (+ (car startPoint) (car endPoint)) 2.0)
                      (/ (+ (cadr startPoint) (cadr endPoint)) 2.0)
                      0.0))

          ;; Calculate polyline length
          (setq polylineLength
                (distance (list (car startPoint) (cadr startPoint) 0.0)
                          (list (car endPoint) (cadr endPoint) 0.0)))

          ;; Calculate block name
          (setq blockName (strcat (rtos (- polylineLength 900) 2 0) "e"))

          ;; Insert block at the calculated midpoint
          ;;;(command "_.INSERT" blockName midpoint 1.0 1.0 0.0) ; Think about running osnaps command statements!
          (if (or
                (tblobjname "BLOCK" blockname)
                (findfile (strcat blockname ".dwg"))
              )
              (command "_.INSERT" blockName "_non" midpoint 1.0 1.0 0.0)
              (alert (strcat "Block '" blockname "' missing"))
          )

          (princ (strcat "\nBlock '" blockName "' placed."))
        )
      )
    )
  (princ "\nError: Please select a valid polyline.")
)


)

 

 
 

 

 

Sebastian

0 Likes

jlee107
Contributor
Contributor
Thank you for your reply and I kinda understand what the problem is now, but lisp still doesn't work and not able to load any type of block. I will ask on special forum.
0 Likes

jlee107
Contributor
Contributor
I have changed the code according to your suggestion. I also ran the test to check if it's retrieving proper information with a fixed value such as 8000. It does return as 7100e, but I'm still having the same issue.
0 Likes

cadffm
Consultant
Consultant

Try my edited code above.

Sample file attached

 

edit

Is it working now?

new Testfile with three different situations

 

 

Another IF to make your program more smart:

 

something like that:

(IF <length of polyline divideable with 100>

    (IF <matching block found>

          insert a new blockreference

          alert 'Block missing'

    )

   alert 'Invalid length'

)

      

Sebastian

0 Likes

jlee107
Contributor
Contributor
Thank you! It does work properly like I want it to.
0 Likes