Jump to content

Insert a line segment into a block


Ogopogo

Recommended Posts

Hi,

Is it possible in Autolisp, by clicking on a line or polyline and it will insert the line segment length into a block?

The block name is "Wall-Tag-FW" and the attribute name in the block to insert the line length is "Length-Ft".

 

Thank you

 

Line Segment Insert.jpg

Link to comment
Share on other sites

Hello Ogopogo, and welcome!

Here is a quick one, it works on LINEs only, not POLYLINEs.

Sorry, I am out of my time. If this works as you expect, I will try to add the POLYLINEs tomorrow or so...

(defun c:pp()
  (setq ent (car (entsel "select a block")))
  (cond ((= (cdr (assoc 2 (entget ent))) "Wall-Tag-FW")
	 (while (/= (cdr (assoc 2 (entget ent))) "LENGTH-FT") (setq ent (entnext ent)))
	 (setq elist (entget ent))
	 (setq line (entget (car (entsel "select line"))))
	 (setq dist (rtos (distance (cdr (assoc 10 line)) (cdr (assoc 11 line)))))
	 (setq elist (subst (cons 1 dist) (assoc 1 elist) elist))
	 (entmod elist)
	 )
	)
  )

 

Link to comment
Share on other sites

Using Vl code much easier.

 

(defun c:pp ( / blk atts obj  att dist)
(setq blk (vlax-ename->vla-object (car (entsel "select block "))))
(if (= (vlax-get blk 'name) "Wall-Tag-FW")
(progn
  (setq atts (vlax-invoke blk 'Getattributes))
  (setq obj  (vlax-ename->vla-object (car (entsel "select p/line"))))
  (setq dist (rtos (vlax-get obj 'length) 2 2))
  (foreach att atts
  (if (= (STRCASE (vlax-get att 'tagstring)) "LENGTH-FT")
   (vlax-put att 'textstring dist)
  )
  )
)
)
(princ)
)
(c:pp)

 

To pick only a block and correct name. 

(setq ss (ssget "+.:S:E" '((0 . "INSERT")(2 . "Wall-Tag-FW"))))
(setq blk (vlax-ename->vla-object (ssname ss 0)))

 

 

Edited by BIGAL
Link to comment
Share on other sites

Hi Fuccaro,

Thank you for help in this. I copied your code and it works. I will wait for your code to handle polylines.

 

Hi Bigal,

Thank you for your help. I copied your codes but having problems using it. Not sure why it is not working.

Link to comment
Share on other sites

My fault changed this in code above. As I tested with one of my dwg's with different tag names.

 

(if (= (STRCASE (vlax-get att 'tagstring)) "LENGTH-FT".")

 

 

Edited by BIGAL
Link to comment
Share on other sites

Hi Fuucaro,

No worries, any time at your convenience.

 

Hi Bigal,

I will try it out tomorrow and let you know.

 

Thank you both for looking into this.

Link to comment
Share on other sites

Hi Bigal,

I think, I am not saving the your codes correctly. I'm copying your codes into WordPad and saving with the extension *.lsp. Is this correct? Or there is something more to do it?

Thank you

Link to comment
Share on other sites

Sounds like Wordpad and Word have hidden characters in the file so the lisp will actually not run. Yes Notepad or a preferred is "Notepad ++" it has built in lisp syntax checking.

Link to comment
Share on other sites

Thank you Fuccaro & Bigal for advising me to use Notepad not Wordpad.

I have installed Notepad ++.

Starting from scratch, I have copied and pasted both codes. Fuccaro's lisp works great. However, Bigal's code isn't working. I have saved Bigal's code as "BigalPP.lsp", see "Screenshot 2023-09-23.jpg". In Autocad, I loaded the lisp file using "Appload", but receive an error message, see "Screenshot 2023-09-23-1.jpg". I wonder if it is ""LENGTH-FT".", is causing the error.? I have very limited knowledge on Autolisp programming.

Once again, I thank you both for helping me.

Screenshot 2023-09-23.jpg

Screenshot 2023-09-23 -1.jpg

Link to comment
Share on other sites

Sorry my fault a typo did not see it crept in somehow when testing

 

Look at your IF line 9 has a extra " and "." 

Should be 

(if (= (STRCASE (vlax-get att 'tagstring)) "LENGTH-FT")

 

Edited by BIGAL
Link to comment
Share on other sites

Hi Bigal,

I have taken out the period "." by coping and pasting your code, see screen shot 2023-09-23-2.

I uploaded the revised "BigalPP.lsp" by "Appload", however there is a different error message  "; error: malformed list on input", see screen shot 203-09-23-03.

I'm sorry for the many texts.

Screenshot 2023-09-23-2.jpg

Screenshot 2023-09-23-3.jpg

Link to comment
Share on other sites

Hi Bigal,

I have copied and pasted the updated code, see below.

However, I'm still getting "error: malformed list on input" when I load the lisp file.

Is what I copied and pasted the correct code?

My apologies, if I'm not correctly following your instructions.

image.thumb.png.45390f32692ac88afd23118a08cdb3b4.png

Screenshot 2023-09-23-3.jpg

Link to comment
Share on other sites

When you post a request like this in future always post a dwg, a problem can be as simple as Capitals in the block name. I have to dummy up a dwg block to test code.

 

Download code again

 

 

Edited by BIGAL
Link to comment
Share on other sites

(defun c:pp( / bent sel enttype attr dist)
  (setq bent (car (entsel "select block")))
  (cond ((= (cdr (assoc 2 (entget bent))) "Wall-Tag-FW")
	 (while (/= (cdr (assoc 2 (entget bent))) "LENGTH-FT") (setq bent (entnext bent)))
	 (setq sel  (entsel "select (p)line")
	       enttype (cdr (assoc 0 (setq line (entget (car sel))))))
	 (cond
	   ((= enttype "LINE") (setq dist (distance (cdr (assoc 10 line)) (cdr (assoc 11 line)))))
	   ((= enttype "LWPOLYLINE") (setq dist (extract sel)))
	   (T (princ "What did you select?!!! "))
	   )
	 (setq attr (entget bent)
	       attr (subst (cons 1 (rtos dist)) (assoc 1 attr) attr)
	       attr (entmod attr)))
	(T (princ "Wrong block!!!" ))
	)
  (princ)
  )
(defun extract(sel / p0 p1 p2 el found dist)
  (setq	p0 (osnap (cadr sel) "_nea")
	el (entget (car sel))
	el (if (= 1 (cdr (assoc 70 el))) (append el (list (assoc 10 el))) el)
	p1 nil p2 nil i -1 found nil)
  (while (and (not found) (< (setq i (1+ i)) (length el)))
    (cond ((= (car (nth i el)) 10) (setq p1 p2 p2 (cdr (nth i el)))))
    (setq found (and p1 (equal (setq dist (distance p1 p2)) (+ (distance p1 p0)(distance p0 p2)) 1e-5)))
    )
  (if found dist nil)  
  )

 

Sorry for answering after so much time!

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...