Jump to content

Pick a point and steal the text inside the polyline.


Anushka

Recommended Posts

Hi guys, how can I save a text from inside a closed polyline in a variable.
I think this has already been answered but I haven't found anything on google,
I would like to click on a point and store the text inside that polyline in a variable.

 

Drawing1.dwg

Link to comment
Share on other sites

How is selecting a point going to do that? Selecting a closed polyline would be easier otherwise you'll have to get a selection set of all closed polyline in the drawing then test each to find if the point is inside it, and if it is get the text. Selecting the closed polyline, you can just get the text.

Link to comment
Share on other sites

@dlanorh 

With this I can more or less select the closest polyline.
However, I need to select the text inside the polyline.
note: I only have a text in this layer inside the polyline

 

 

(defun c:Test ( / pt ss in sn cl ls)
 ;; Tharwat - Date: 06.Jun.2017	;;
 (if (and (setq pt (getpoint "\nPick a point :"))
          (setq ss (ssget "_X" '((0 . "*POLYLINE")(8 . "testA"))))
        )
 (progn
   (repeat (setq in (sslength ss))
     (setq sn (ssname ss (setq in (1- in)))
           cl (vlax-curve-getclosestpointto sn pt)
           ls (cons (list (distance cl pt) sn) ls)
           )
     )
   (sssetfirst nil (ssadd (cadar (vl-sort ls '(lambda (a b) (< (car a) (car b)))))))
   )
 )
(princ)
)

 

Link to comment
Share on other sites

The code you have doesn't test if a point is inside a closed polyline, it finds the nearest polyline. If you select a point and its closest point is on a common border of two polylines the distance to both polylines will be the same, and you therefore may get the wrong polyline returned. That aside this adaptation of @Tharwat code will save the text into variable txt

 

(defun c:Test ( / pt ss in sn cl ls ent vlst tss)
 ;; Tharwat - Date: 06.Jun.2017	;;
  (if (and (setq pt (getpoint "\nPick a point :"))
          (setq ss (ssget "_X" '((0 . "*POLYLINE")(8 . "testA"))))
      )
    (progn
      (repeat (setq in (sslength ss))
        (setq sn (ssname ss (setq in (1- in)))
              cl (vlax-curve-getclosestpointto sn pt)
              ls (cons (list (distance cl pt) sn) ls)
        )
      )
      (setq ent (cadar (vl-sort ls '(lambda (a b) (< (car a) (car b)))))
            vlst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget ent)))
            tss (ssget "_WP" vlst '((0 . "*TEXT")))
            txt (cdr (assoc 1 (entget (ssname tss 0))))
      );end_setq
    )
  )
  (princ)
)

 

txt will be a global and available once the lisp has finished

 

 

 

Link to comment
Share on other sites

23 minutes ago, Anushka said:

This is not working the text is not correct: /
how to test to get the correct text ??

 

This tells me nothing! How is the text not correct. I tested on the drawing you supplied. Try (princ txt) on the command line after the lisp is run or inset (princ txt) before the last (princ) in the file.

Link to comment
Share on other sites

6 minutes ago, Anushka said:

This is not working the text is not correct: /
how to test to get the correct text ??

 

Just as dlanorh said, the routine Tharwat provided does not check if a point is inside a polyline, but rather the nearest polyline to the point.

 

Perhaps the below picture will give you a better idea:

 

image.png.fa7440df8f6b59c52c04eba2e58ec734.png

 

Whatever is the nearest polyline to the X mark will be the one selected. If you choose 1, then C is returned. But if you choose 3, then B is returned because that's the closer polyline. 

 

Also technically speaking, text B and C is also inside the outside larger polyline if this is your case, so clicking on Point 4 will give you some weird results.

 

But anyway, if you're following Tharwat's routine, then I've modified a few things

(defun c:Test ( / *error* pt ss in sn cl ls mdis osm ent maxpt minpt osm pl RemoveAdjacentDuplicates txt)	; Variable modified by Jonathan Handojo
  ;; Tharwat - Date: 06.Jun.2017	;;
  
  (defun *error* (msg) (setvar "OSMODE" osm) (princ (strcat "Error: " msg)))	; Added by Jonathan Handojo
  
  (defun RemoveAdjacentDuplicates (lst / tst)	; Function Added by Jonathan Handojo
    (setq tst lst)
    (vl-remove nil (mapcar '(lambda (x) (if (null (equal x (car (setq tst (cdr tst))) 1e-8)) x)) lst))
    )
  
  ;; Snap adjusted by Jonathan Handojo
  (setq osm (getvar "OSMODE"))
  (setvar "OSMODE" 0)
  (if (and (setq pt (getpoint "\nPick a point :"))
	   (setq ss (ssget "_X" '((0 . "*POLYLINE"))))
	   )
    (progn
      (repeat (setq in (sslength ss))
	(setq sn (ssname ss (setq in (1- in)))
	      cl (vlax-curve-getclosestpointto sn pt)
	      ls (cons (list (distance cl pt) sn) ls)
	      mdis (apply 'min (mapcar 'car ls))	; Added by Jonathan Handojo
	      )
	)
      
      (setq ent (cadar (vl-member-if '(lambda (x) (equal mdis (car x) 1e-8)) ls)))	; Modified by Jonathan Handojo
      
    ;; Below is modification added

      (vla-ZoomWindow (vlax-get-acad-object)
	(progn
	  (vla-GetBoundingBox (vlax-ename->vla-object ent) 'minpt 'maxpt)
	  minpt
	  )
	maxpt
	)
      
      (setq txt (ssget "_WP"
		       (RemoveAdjacentDuplicates
			 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (eq (car x) 10)) (entget ent)))
			 )
		       '((0 . "TEXT,MTEXT"))
		       )
	    txt (if txt (cdr (assoc 1 (entget (ssname txt 0)))))
	    )

      (vla-ZoomPrevious (vlax-get-acad-object))
      )
    )
  (setvar "OSMODE" osm)
  (if txt (princ (strcat "\n" txt)))
  (princ)
  )

 

Link to comment
Share on other sites

8 minutes ago, dlanorh said:

 

This tells me nothing! How is the text not correct. I tested on the drawing you supplied. Try (princ txt) on the command line after the lisp is run or inset (princ txt) before the last (princ) in the file.

 

 

Let's calm... remember you said:

 

47 minutes ago, dlanorh said:

If you select a point and its closest point is on a common border of two polylines the distance to both polylines will be the same, and you therefore may get the wrong polyline returned.

 

That's the reason the result is not correct. When you do vl-sort, there are two distances that are the same value, so it doesn't know which point it's touching.

Link to comment
Share on other sites

Ignore my code above, below is a more robust approach:

 

(defun c:Test ( / *error* insidepoly RemoveAdjacentDuplicates ent maxpt minpt osm pt ss txt)	; Variable modified by Jonathan Handojo
  ;; Tharwat - Date: 06.Jun.2017	;;
  
  (defun *error* (msg) (setvar "OSMODE" osm) (princ (strcat "Error: " msg)))	; Added by Jonathan Handojo

  (defun insidepoly (pt ent ray / ints)
    (setq ints (vlax-invoke (vlax-ename->vla-object ent) 'intersectwith ray acExtendNone))
    (if (eq (rem (length ints) 6) 3) ent)
    )
  
  (defun RemoveAdjacentDuplicates (lst / tst)	; Function Added by Jonathan Handojo
    (setq tst lst)
    (vl-remove nil (mapcar '(lambda (x) (if (null (equal x (car (setq tst (cdr tst))) 1e-8)) x)) lst))
    )
  
  ;; Snap adjusted by Jonathan Handojo
  (setq osm (getvar "OSMODE"))
  (setvar "OSMODE" 0)
  (if (and (setq pt (getpoint "\nPick a point :"))
	   (setq ss (ssget "_X" '((0 . "*POLYLINE") (8 . "testA"))))
	   (setq ray (vla-AddRay (vla-get-ModelSpace (vla-get-ActiveDocument (vlax-get-acad-object))) (vlax-3d-point pt) (vlax-3d-point (polar pt 0 1))))
	   (setq ent (vl-some '(lambda (x) (insidepoly pt x ray)) (JH:selset-to-list ss)))	; Modified by Jonathan Handojo
	   )
    (progn
      (vla-ZoomWindow (vlax-get-acad-object)
	(progn
	  (vla-GetBoundingBox (vlax-ename->vla-object ent) 'minpt 'maxpt)
	  minpt
	  )
	maxpt
	)
      
      (setq txt (ssget "_WP"
		       (RemoveAdjacentDuplicates
			 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (eq (car x) 10)) (entget ent)))
			 )
		       '((0 . "TEXT,MTEXT"))
		       )
	    txt (if txt (cdr (assoc 1 (entget (ssname txt 0)))))
	    )

      (vla-ZoomPrevious (vlax-get-acad-object))
      )
    
    ;; Below is modification added
    )
  (vla-Delete ray)
  (setvar "OSMODE" osm)
  (if txt (princ (strcat "\n" txt)))
  (princ)
  )

(defun JH:selset-to-list (selset / lst iter) ; Returns all entities within a selection set into a list.
  (setq iter 0)
  (repeat (sslength selset)
    (setq lst (cons (ssname selset iter) lst)
	  iter (1+ iter))
    )
  (reverse lst)
  )

 In my case, it prints the text to the command line

  • Thanks 1
Link to comment
Share on other sites

this mark is the name of the neighborhood.
I have several posts within these markings, I mean to which neighborhood each point belongs.

Link to comment
Share on other sites

2 minutes ago, Jonathan Handojo said:

Ignore meu código acima, abaixo está uma abordagem mais robusta:

 


(defun c:Test ( / *error* insidepoly RemoveAdjacentDuplicates ent maxpt minpt osm pt ss txt)	; Variable modified by Jonathan Handojo
  ;; Tharwat - Date: 06.Jun.2017	;;
  
  (defun *error* (msg) (setvar "OSMODE" osm) (princ (strcat "Error: " msg)))	; Added by Jonathan Handojo

  (defun insidepoly (pt ent ray / ints)
    (setq ints (vlax-invoke (vlax-ename->vla-object ent) 'intersectwith ray acExtendNone))
    (if (eq (rem (length ints) 6) 3) ent)
    )
  
  (defun RemoveAdjacentDuplicates (lst / tst)	; Function Added by Jonathan Handojo
    (setq tst lst)
    (vl-remove nil (mapcar '(lambda (x) (if (null (equal x (car (setq tst (cdr tst))) 1e-8)) x)) lst))
    )
  
  ;; Snap adjusted by Jonathan Handojo
  (setq osm (getvar "OSMODE"))
  (setvar "OSMODE" 0)
  (if (and (setq pt (getpoint "\nPick a point :"))
	   (setq ss (ssget "_X" '((0 . "*POLYLINE") (8 . "testA"))))
	   (setq ray (vla-AddRay (vla-get-ModelSpace (vla-get-ActiveDocument (vlax-get-acad-object))) (vlax-3d-point pt) (vlax-3d-point (polar pt 0 1))))
	   (setq ent (vl-some '(lambda (x) (insidepoly pt x ray)) (JH:selset-to-list ss)))	; Modified by Jonathan Handojo
	   )
    (progn
      (vla-ZoomWindow (vlax-get-acad-object)
	(progn
	  (vla-GetBoundingBox (vlax-ename->vla-object ent) 'minpt 'maxpt)
	  minpt
	  )
	maxpt
	)
      
      (setq txt (ssget "_WP"
		       (RemoveAdjacentDuplicates
			 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (eq (car x) 10)) (entget ent)))
			 )
		       '((0 . "TEXT,MTEXT"))
		       )
	    txt (if txt (cdr (assoc 1 (entget (ssname txt 0)))))
	    )

      (vla-ZoomPrevious (vlax-get-acad-object))
      )
    
    ;; Below is modification added
    )
  (vla-Delete ray)
  (setvar "OSMODE" osm)
  (if txt (princ (strcat "\n" txt)))
  (princ)
  )

(defun JH:selset-to-list (selset / lst iter) ; Returns all entities within a selection set into a list.
  (setq iter 0)
  (repeat (sslength selset)
    (setq lst (cons (ssname selset iter) lst)
	  iter (1+ iter))
    )
  (reverse lst)
  )

No meu caso, ele imprime o texto para a linha de comando

 

@Jonathan Handojo it works!!! thank you very much!!!

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