need help with lisp

need help with lisp

bouchair_mehdi
Explorer Explorer
1,809 Views
16 Replies
Message 1 of 17

need help with lisp

bouchair_mehdi
Explorer
Explorer

hello everyone,

I have work that needs to create the starting vertex of a polyline inside the contour not outside, I have a lisp but when I load it into AutoCAD it appears an error message (; error: syntax error), please can someone help me for this :

(defun c:polyInsideContour (/ pt obj) (setq obj (car (entsel "\nSelect the region boundary: "))) (if (not (eq 'closed (cdr (assoc 70 (entget obj))))) (princ "\nSelected object is not a closed polyline.") (setq pt (getpoint "\nSpecify starting point inside the region: ")) (if (inRegion obj pt) (progn (command "_.pline" "_Non" pt "") ; create the polyline with no vertices (setq pline (entlast)) ; select the last object (the polyline) (if (inRegion pline pt) (command (strcat "_Polyline" " " "C" " " pt " "))) ; add a vertex at the starting point (entdel pline) (princ "\nStarting point must be inside the region.") ) (princ "\nStarting point must be inside the region.") ) ) ) (defun inRegion (obj pt) (setq res 0) (if (and (eq 'closed (cdr (assoc 70 (entget obj)))) (eq "LWPOLYLINE" (cdr (assoc 0 (entget obj))))) (progn (setq vertices (cdr (assoc 10 (entget obj)))) (setq n (length vertices)) (setq j (- n 1)) (setq x pt) (setq x1 (car vertices)) (repeat n (setq x2 (car (nth j vertices))) (setq y2 (cadr (nth j vertices))) (setq y1 (cadr (nth (- j 1) vertices))) (if (> y1 y2) (setq tmp y1 y1 y2 y2 tmp)) (if (and (<= y1 x) (< y2 x) (< x2 x)) (if (/= tmp 0) ; check if point is on the edge (setq res (- res 1))) (setq j (- j 1)) ) ) (princ "\nSelected object is not a closed LWPOLYLINE.") (setq res 0) ) (if (< (abs res) 1) ; check if point is inside or outside the region (setq inside T) (setq inside nil) ) inside )

0 Likes
1,810 Views
16 Replies
Replies (16)
Message 2 of 17

ВeekeeCZ
Consultant
Consultant

You cannot have a program formatted as a single paragraph. It's not interpreted right.

 

Go to the MS Word and replace ; with ^p; and ( with ^p(

Message 3 of 17

Kent1Cooper
Consultant
Consultant

All sorts of problems....  Some that I noticed [there may be more]:

 

The first (if) function needs its 'else' argument, which is a series of operations, combined into a single argument with a (progn) wrapper.  The same is true for the 'then' argument later following (if (inRegion obj pt).

 

(eq 'closed (cdr (assoc 70 (entget obj))))

[twice] is invalid -- (cdr (assoc 70 (entget obj))) will return an integer, a bit-coded value that will contain the 1 bit if the Polyline is closed.

 

(command "_.pline" "_Non" pt "") ; create the polyline with no vertices

is no good [aside from the fact that if it worked, it would have one vertex] -- try it manually, and you'll see that no Polyline results.  That kills the rest of it.

 

(command (strcat "_Polyline" " " "C" " " pt " "))) ; add a vertex at the starting point

is invalid.  A string argument at the beginning of a (command) function can only be a command name.  You seem to be confusing AutoLisp syntax with command-macro syntax.  Do you intend a PEDIT command?

 

(setq vertices (cdr (assoc 10 (entget obj))))

will get you only the first vertex, not all of them, which kills the rest of it.

 

I didn't study the workings of (inRegion) in detail, but it looks like it's missing a (progn) wrapper and a right parenthesis.

 

(if (and (<= y1 x) (< y2 x) (< x2 x))

is invalid, because y1, y2 and x2 will be numbers, but x will be a list of three numbers.

 

And I'm not sure, but it looks like its analysis is designed to test whether a point is inside a rectangular area, not a Polyline of any shape.

 

Kent Cooper, AIA
Message 4 of 17

komondormrex
Mentor
Mentor

hey,

is it ai generated code?

Message 5 of 17

bouchair_mehdi
Explorer
Explorer

yes

0 Likes
Message 6 of 17

Kent1Cooper
Consultant
Consultant

No wonder....

Kent Cooper, AIA
0 Likes
Message 7 of 17

komondormrex
Mentor
Mentor

and what is exact objective of it? start a polyline inside a closed one? 

0 Likes
Message 8 of 17

pbejse
Mentor
Mentor

@komondormrex wrote:

is it ai generated code?


You called it!

0 Likes
Message 9 of 17

Sea-Haven
Mentor
Mentor

Paste into word do the replace then copy and paste into notepad then save. If save from Word can still have all the hidden word stuff in lisp

 

 

0 Likes
Message 10 of 17

Sea-Haven
Mentor
Mentor

As I said before you pay to generate AI code will the poster pay to get it all fixed up ?

 

Please state in future if code was generated by AI will make much easier to decide if worth looking at.

 

Post a dwg of what you want before and after you may have got working code by now, NOT DONE IN AI.

0 Likes
Message 11 of 17

bouchair_mehdi
Explorer
Explorer

the lisp is for creating the start point of a polyline closed inside it

0 Likes
Message 12 of 17

Sea-Haven
Mentor
Mentor

Post a dwg or image at least.

0 Likes
Message 13 of 17

bouchair_mehdi
Explorer
Explorer

create the first vertex inside the closed polyline

0 Likes
Message 14 of 17

bouchair_mehdi
Explorer
Explorer

creating the first vertex inside a closed polyline

0 Likes
Message 15 of 17

CADaSchtroumpf
Advisor
Advisor

I have this which is approaching.
Maybe chatGPT can improve it...😉

(vl-load-com)
(defun clockwise-p (p1 p2 p3)
  (< (sin (- (angle p1 p3) (angle p1 p2))) -1e-14)
)
(defun add_vtx (obj add_pt ent_name / bulg)
  (vla-addVertex
    obj
    (1+ (fix add_pt))
    (vlax-make-variant
      (vlax-safearray-fill
        (vlax-make-safearray vlax-vbdouble (cons 0 1))
          (list
            (car (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
            (cadr (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
          )
      )
    )
  )
  (setq bulg (vla-GetBulge obj (fix add_pt)))
  (vla-SetBulge obj
    (fix add_pt)
    (/
      (sin (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
      (cos (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
    )
  )
  (vla-SetBulge obj
    (1+ (fix add_pt))
    (/
      (sin (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
      (cos (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
    )
  )
  (vla-update obj)
)
(defun c:appendix ( / js obj_vla e_name AcDoc rad obj_orig pt pto pt_prmt fill_prmt blg)
  (while (null (setq js (ssget "_+.:E:S" '((0 . "LWPOLYLINE")))))
    (princ "\nThis is not a valid object!")
  )
  (setq obj_vla (vlax-ename->vla-object (setq e_name (ssname js 0))) AcDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (vla-StartUndoMark AcDoc)
  (initget 4)
  (setq rad (getdist "\nEnter a fillet radius? <0.0>: "))
  (if (not rad) (setq rad 0.0))
  (setq obj_orig (vla-Copy obj_vla))
  (sssetfirst nil js)
  (while (setq pt (getpoint "\nPosition of new vertex: "))
    (setq
      pto (vlax-curve-getClosestPointTo obj_orig (trans pt 1 0))
      pt_prmt (vlax-curve-getparamatpoint obj_vla pto)
      fill_prmt (vlax-curve-getdistatparam obj_vla pt_prmt)
    )
    (cond
      ((vlax-curve-getparamatdist obj_vla (- fill_prmt rad))
        (if (clockwise-p (vlax-curve-getpointatparam obj_vla (vlax-curve-getparamatdist obj_vla (- fill_prmt rad))) pto (trans pt 1 0))
          (setq blg (- (1- (sqrt 2))))
          (setq blg (1- (sqrt 2)))
        )
        (add_vtx obj_vla (vlax-curve-getparamatdist obj_vla (- fill_prmt rad)) e_name)
        (add_vtx obj_vla (vlax-curve-getparamatdist obj_vla (+ fill_prmt rad)) e_name)
        (vla-SetBulge obj_vla (1+ (fix pt_prmt)) blg)
        (vla-update obj_vla)
        (mapcar
          '(lambda (p r / )
            (vla-addVertex
              obj_vla
              (+ r (fix pt_prmt))
              (vlax-make-variant
                (vlax-safearray-fill
                  (vlax-make-safearray vlax-vbdouble (cons 0 1))
                  (list
                    (car (trans (trans p 1 0) 0 e_name))
                    (cadr (trans (trans p 1 0) 0 e_name))
                  )
                )
              )
            )
            (vla-update obj_vla)
          )
          (list (polar pto (angle pto (trans pt 1 0)) rad) pt (polar pto (angle pto (trans pt 1 0)) rad))
          (list 2 3 4)
        )
        (vla-SetBulge obj_vla (+ 4 (fix pt_prmt)) blg)
        (vla-update obj_vla)
        (sssetfirst nil js)
      )
      (T (princ "\nThe fillet radius is too large for this segment"))
    )
  )
  (sssetfirst)
  (vla-Delete obj_orig)
  (vla-EndUndoMark AcDoc)
  (prin1)
)
Message 16 of 17

greg_battin
Advocate
Advocate
The lisp found at the following link lets you specify a new origin of a polyline (or 3D polyline).
All credit goes to T.Willey
http://www.theswamp.org/index.php?topic=12624.msg154976#msg154976

It was also featured here:
https://autocadtips1.com/2014/06/25/autolisp-reorder-polyline-origin/
0 Likes
Message 17 of 17

bouchair_mehdi
Explorer
Explorer

HAT IS THE COMMAND