Jump to content

Help with Dimension and Text Code lisp


maahee

Recommended Posts

How to calculate angle between two point using polar function or alternatives  

I work another code I unable to create clickable option in command line

Edited by maahee
Link to comment
Share on other sites

(defun c:block-area ()
  (vl-load-com)
  
  ; Step 1: Select a polyline and extract its length and width
  (setq ss (ssget '((0 . "LWPOLYLINE"))))
  (if (/= (sslength ss) 1) (return)) ; exits function if more or less than 1 polylline selected
  (setq ent (ssname ss 0))
  (setq data (entget ent))
  (setq p1 (cdr (assoc 10 data)))
  (setq p2 (vlax-curve-getStartPoint ent))
  (setq p3 (vlax-curve-getEndPoint ent))
  (setq width (distance p2 p3))
  (setq length (cdr (assoc 40 data)))
  
  ; Step 2: Assign an alphabet to a variable based on the polyline selection
  (setq alphabet (cond 
              ((entget ent '((0 . "INSERT") (66 . 1))) '(2 . "BLOCK A")) ;if polyline is identified as "BLOCK A"
              ((entget ent '((0 . "INSERT") (66 . 2))) '(2 . "BLOCK B")) ;if polyline is identified as "BLOCK B"
              ((entget ent '((0 . "INSERT") (66 . 3))) '(2 . "BLOCK C")) ;if polyline is identified as "BLOCK C"
              (t ""))) ;if not recognized, the alphabet will be empty string
  
  ; Step 3: Create a text at the center of the polyline
  (setq center (polar p1 (/ pi 2) (/ width 2))) ; calculates center point
  (setq text (strcat "BLOCK " (cdr alphabet))) ; assigns text string
  (setq textent (entmakex (list '(0 . "TEXT") (cons 10 center) '(40 . 2) (cons 1 text)))))
  (entupd textent)
  
  ; Step 4: Calculate the area of the polyline
  (setq area (* width length))
  
  ; Step 5: Add a field to display the area in the text
  (setq fieldexpr (strcat "AREA OF BLOCK " (cdr alphabet) ": " (rtos length 2 2) " X " (rtos width 2 2) " = " (rtos area 2 2))))
  (setq fieldcode (cons 2 fieldexpr))
  (setq fieldtext (entmakex (list '(0 . "FIELD") (cons 11 center) fieldcode))))
  (entupd fieldtext)
  
  ; Step 6: Add dimensions to the polyline
  (setq dim1point1 (list p2 (nth 1 p2))) ; start point of the first dimension
  (setq dim1point2 (list center (nth 1 center))) ; end point of the first dimension
  (dimaligned dim1point1 dim1point2 p3)
  (setq dim2point1 (list center (nth 0 center))) ; start point of the second dimension
  (setq dim2point2 (list p3 (nth 0 p3))) ; end point o
f the second dimension
  (dimaligned dim2point1 dim2point2 p2)
)

I would like the lisp to work like this:

 

Step 1

1. select polyline and measure length and width 

 

Step2

2. text give alphabet for polyline at center 

 

Step 3

3. Arrange length and width in text format “AREA OF BLOCK A : LENGTH X WIDTH =AREA”

 

Step 4

4. All should be field property for "LENGTH X WIDTH =AREA"

 

Step 5

5. Add dimensions to the polyline 

 

Step 6

6. Above step repeated each other polyline with new alphabet 

 

 

Code not work

 

Edited by SLW210
Removed Untagged Code!
Link to comment
Share on other sites

USE THE <> for your code. Please. Admin will get upset.

 

Are you just wanting to pick plines and label them “AREA OF BLOCK A : LENGTH X WIDTH =AREA”, using fields update if shape changed. Ok can do label = "A" next will be "B" next will be "C" etc yes A-Z is 26 but can do label AA AB AC then. “AREA OF BLOCK A : LENGTH x WIDTH Area = 400”

 

There is a lot of area label plines start at Lee-mac.com he has a good one. Then add the field calc to your area label.

 

Same with dimensions of pline google Kent Cooper label plines. He is over at forums/autodesk has multiple versions.

 

Other comments

Step 1 not required other than select plines

Step 2 is all wrong sorry just totally incorrect method needs full recode. Explain more post dwg

Step 3 ; calculates center point (setq pt (osnap (vlax-curve-getStartPoint obj) "gcen"))

Step4 (vla-get-area pline) & Step 5 lee-mac area as field

Step 6 Kent Cooper

Step 7 Make new label

 

Did you use ChatGP ? Please let us know.

Edited by BIGAL
Link to comment
Share on other sites

  • 2 weeks later...
 

(setq all_objs (ssget "_X"))

(command "_.mirror" all_objs pt1 pt2"")

 

I use above code, this code mirror all object present in drawing. I need only mirror code generate object 

Edited by maahee
Link to comment
Share on other sites

If you look at ssadd and create a selection set with that, would it work? perhaps

... some code

(setq New_Objs (ssadd)) ; create blank / emp[ty selection set

.....some more code, create an object

(ssadd (entlast) New_Objs)

.. some more code


(command '_Mirror.....

 

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

another way if you are creating alot of things at once.

 

(setq ssnew (ssadd)) ;blank selection set.

(setq LastEnt (entlast)) ;put this before anything is created or modified.
;rest of your code

(if (setq en (entnext LastEnt)) ;pick up anything made or modified in the drawing since LastEnt was defined.
  (while en
    (ssadd en ssnew)
    (setq en (entnext en))
  )
)
(command "_.mirror" ssnew pt1 pt2"") ;mirror those objects in the drwaing.

 

Edited by mhupp
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

(setq SelObjs (ssadd)) ; create blank / empty selection set 

  (setq p1 (getpoint "\nEnter first point: "))

  (setq p2 (getpoint p1 "\nEnter second point: "))

  

 

  (setq p3 (polar p2 (+ (angle p1 p2) (* pi 1.5)) Dp))

  (setq p4 (polar p3 (+ (angle p2 p3) (* pi 1.5)) WT))

  (setq p5 (polar p4 (+ (angle p3 p4) (* pi 1.5)) (- Dp 0.15)))

  (setq p6 (polar p5 (+ (angle p4 p5) (/ pi 2)) (- (distance p1 p2) (* 2 WT))))

  (setq p7 (polar p6 (+ (angle p5 p6) (/ pi 2)) (- Dp 0.15)))

 (setq p8 (polar p7 (+ (angle p6 p7) (* pi 1.5)) WT))

 

 (setq SelObjs (ssadd)) ; create blank / empty selection set

 

 (command "line" p1 p2 p3 p4 p5 p6 p7 p8 p1"")

 

 (ssadd (entlast) SelObjs)

 

 (command "_chprop" "SelObjs" "LA" slab) ; set into layer.

 

 (command "_-hatch" "SelObjs" "angle" "Yes" "" 0.5 "patname" "ANSI31" "") ;hatch those

objects in the drawing.

Link to comment
Share on other sites

(setq SelObjs (ssadd)) ; create blank / empty selection set 
  (setq p1 (getpoint "\nEnter first point: "))
  (setq p2 (getpoint p1 "\nEnter second point: "))
  
  (setq p3 (polar p2 (+ (angle p1 p2) (* pi 1.5)) Dp))
  (setq p4 (polar p3 (+ (angle p2 p3) (* pi 1.5)) WT))
  (setq p5 (polar p4 (+ (angle p3 p4) (* pi 1.5)) (- Dp 0.15)))
  (setq p6 (polar p5 (+ (angle p4 p5) (/ pi 2)) (- (distance p1 p2) (* 2 WT))))
  (setq p7 (polar p6 (+ (angle p5 p6) (/ pi 2)) (- Dp 0.15)))
 (setq p8 (polar p7 (+ (angle p6 p7) (* pi 1.5)) WT))
 (setq SelObjs (ssadd)) ; create blank / empty selection set
 (command "line" p1 p2 p3 p4 p5 p6 p7 p8 p1"")
 (ssadd (entlast) SelObjs)
 (command "_chprop" "SelObjs" "LA" slab) ; set into layer.
 (command "_-hatch" "SelObjs" "angle" "Yes" "" 0.5 "patname" "ANSI31" "") ;hatch those
objects in the drawing.

 

Link to comment
Share on other sites

(setq SelObjs (ssadd)) ; create blank / empty selection set 
  (setq p1 (getpoint "\nEnter first point: "))
  (setq p2 (getpoint p1 "\nEnter second point: "))
  
  (setq p3 (polar p2 (+ (angle p1 p2) (* pi 1.5)) Dp))
  (setq p4 (polar p3 (+ (angle p2 p3) (* pi 1.5)) WT))
  (setq p5 (polar p4 (+ (angle p3 p4) (* pi 1.5)) (- Dp 0.15)))
  (setq p6 (polar p5 (+ (angle p4 p5) (/ pi 2)) (- (distance p1 p2) (* 2 WT))))
  (setq p7 (polar p6 (+ (angle p5 p6) (/ pi 2)) (- Dp 0.15)))
 (setq p8 (polar p7 (+ (angle p6 p7) (* pi 1.5)) WT))
 (setq SelObjs (ssadd)) ; create blank / empty selection set
 (command "line" p1 p2 p3 p4 p5 p6 p7 p8 p1"")
 (ssadd (entlast) SelObjs)
 (command "_chprop" "SelObjs" "LA" slab) ; set into layer.
 (command "_-hatch" "SelObjs" "angle" "Yes" "" 0.5 "patname" "ANSI31" "") ;hatch those
objects in the drawing.

(command "_chprop" "SelObjs" "LA" slab) ; set into layer.

 

(command "_-hatch" "SelObjs" "angle" "Yes" "" 0.5 "patname" "ANSI31"

 

Invalid selection Error show I need help 

Edited by maahee
Link to comment
Share on other sites

close - you're getting there. A couple of little changes then

You have (setq SelObjs (ssadd)) twice in the example, it doesn't affect anything just adds clutter to the code

(command "Line"..... creates 8 separate lines as you would do using the command in AutoCAD.

You next line (ssadfd (entlast) SelObjs) adds the last entity created to the selection set - the line P8 to P1 (and not the whole lot)

The simple fix to understand and get working would be to write a line for each segment of the lines and add these to the selection set:

 

 (command "line" p1 p2 "")
 (ssadd (entlast) SelObjs)
 (command "line" p2 p3 "")
 (ssadd (entlast) SelObjs)
 (command "line" p3 p4"")
 (ssadd (entlast) SelObjs)

.....and so on

 

That makes the code a bit longer and less efficient (in the scale if milliseconds and not 'get a coffee' inefficiency

Another way would be to set a marker (setq latestent (entlast)) for example, create the lines as above and then loop forward from the latestent entity adding them to the selection set MHUPP gave an answer earlier today showing this method if you want to look at that

 

A quick correction to the chprop line:

(command "_.chprop" SelObjs "" "LA" "slab" "") ; set into layer.

 

You had SelObjs as "SelObjs" .Within " " it is seen as text, and outside " " it is seen as a variable - you want to use a variable here. then of course, add an 'enter' to stop the selections with "", also the same to end the chprop command

 

 

and so on...

 

Similar things with Hatch, go through it in the command line, type in -hatch and follow the prompts, copying exactly what you type into the LISP line for the hatch and you should get there... but so close!!

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

12 hours ago, Steven P said:

close - you're getting there. A couple of little changes then

You have (setq SelObjs (ssadd)) twice in the example, it doesn't affect anything just adds clutter to the code

(command "Line"..... creates 8 separate lines as you would do using the command in AutoCAD.

You next line (ssadfd (entlast) SelObjs) adds the last entity created to the selection set - the line P8 to P1 (and not the whole lot)

The simple fix to understand and get working would be to write a line for each segment of the lines and add these to the selection set:

 

 (command "line" p1 p2 "")
 (ssadd (entlast) SelObjs)
 (command "line" p2 p3 "")
 (ssadd (entlast) SelObjs)
 (command "line" p3 p4"")
 (ssadd (entlast) SelObjs)

.....and so on

 

That makes the code a bit longer and less efficient (in the scale if milliseconds and not 'get a coffee' inefficiency

Another way would be to set a marker (setq latestent (entlast)) for example, create the lines as above and then loop forward from the latestent entity adding them to the selection set MHUPP gave an answer earlier today showing this method if you want to look at that

 

A quick correction to the chprop line:

(command "_.chprop" SelObjs "" "LA" "slab" "") ; set into layer.

 

You had SelObjs as "SelObjs" .Within " " it is seen as text, and outside " " it is seen as a variable - you want to use a variable here. then of course, add an 'enter' to stop the selections with "", also the same to end the chprop command

 

 

and so on...

 

Similar things with Hatch, go through it in the command line, type in -hatch and follow the prompts, copying exactly what you type into the LISP line for the hatch and you should get there... but so close!!

Thanks sir

Link to comment
Share on other sites

You need to go back to 1st principles of geometry for red rectangs, I would work out right hand point on purple line, then a little tricky but work out point on white line. You have a couple of knowns like the distance of short red perp line. so using geometry can work out the 4 points. Then use a wipeout much easier than trimming around the rectang. Think Pythagoras or sine theory.

 

image.png.3340c795be9be8d5d65f3caf8ab6ed27.png

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...