Jump to content

Recommended Posts

Posted
(if (= (cdr (assoc 0 bm)) "LWPOLYLINE") ; Check if the entity type is LWPOLYLINE
  (progn
      (setq pline (cdr (assoc -1 bm)))
      (setq pdb (entget pline)) ; Get the entity list for the selected polyline.
      (foreach sublist pdb
        (if (= (car sublist) 10) ; Look for vertex points (coded as 10).
          (progn
            (setq p1 (cdr sublist)) ; Get the point coordinates.
            (setq p2 (cadr sublist))   

		(command "_.chprop" pline "" "_lw" "10.00" "")
                    
            (command "DIMALIGNED" p1 p2 "@0.35<180") ; Create aligned dimension.
          ) ; end progn
        ) ; end if
      ) ; end foreach
    ) ; end progn

) ; if

            (setq p2 (cadr sublist))          I can't  find the second coordinate of the polyline

Posted

The second point will be encountered in another iteration of the foreach loop; you should construct a list of the points encountered within the foreach loop and then access the first & second elements of the list using car & cadr, outside of the foreach loop.

  • Thanks 1
Posted (edited)
(command "DIMALIGNED" "" "" "@1,1")

 

Edited by maahee
Posted
  On 2/22/2025 at 12:41 PM, Lee Mac said:

The second point will be encountered in another iteration of the foreach loop; you should construct a list of the points encountered within the foreach loop and then access the first & second elements of the list using car & cadr, outside of the foreach loop.

Expand  

can I use (DIMALIGNED double enter then select polyline segment) 

(command "DIMALIGNED" "" "" "@1,1")

 

Posted

Just make the iteration skip ahead while pt1 and pt2 are not assigned

 

(if (= (cdr (assoc 0 bm)) "LWPOLYLINE") ; Check if the entity type is LWPOLYLINE
  (progn
      (setq pline (cdr (assoc -1 bm)))
      (setq pdb (entget pline)) ; Get the entity list for the selected polyline.
      (foreach sublist pdb
        (if (= (car sublist) 10) ; Look for vertex points (coded as 10).
          (progn
	    (if pt2
	      (setq pt1 pt2 pt2 (cdr sublist))
	      (if pt1
		(setq pt2 (cdr sublist))
		(setq pt1 (cdr sublist))
	      )
	    )
		
	      
;;;            (setq p1 (cdr sublist)) ; Get the point coordinates.
;;;            (setq p2 (cadr sublist))  

;;;(command "_.chprop" pline "" "_lw" "10.00" "")
            (if (and pt1 pt2)
              (command "DIMALIGNED" pt1 pt2 "@0.35<180") ; Create aligned dimension.
	    )
          ) ; end progn
        ) ; end if
      ) ; end foreach
    ) ; end progn

)

 

  • Thanks 1
Posted (edited)

I disabled the line '(command "_.chprop" pline "" "_lw" "10.00" "")' it only needs to be executed once.
Place it before 'foreach'

Edited by GLAVCVS
  • Thanks 1
Posted

This code means that pt1 and pt2 should be 'nil' at the beginning of each polyline process

  • Thanks 1
Posted
  On 2/22/2025 at 1:03 PM, maahee said:

can I use (DIMALIGNED double enter then select polyline segment) 

(command "DIMALIGNED" "" "" "@1,1")

 

Expand  

 

You seem to have ignored my suggestion.

  • Like 1
Posted
  On 2/22/2025 at 3:11 PM, Lee Mac said:

 

You seem to have ignored my suggestion.

Expand  

I can't understand how to do it. use of nested foreach. I am beginner of autolisp language 

Posted

Steps as Lee Mac Suggests

- Use ForEach to create a list of points (assoc 10)

- End ForEach

- Loop through list of points (n-1) times where n is the list length

- Put in your dimension

- End Loop

 

Often there is a function out there that can 'plug and play' into a routine... below is a subfunction massoc which I find really handy, often using the 2nd option in the 2nd post... 

'Key' in your case will be 10

lst in your case will be pdb (though you might be able to also use (entget bm)

 

The return is a list of all the keys, in your case the points, I think in the format (10 pt-X pt-Y pt-Z), so (CADR... to get the points only.

 

 

 

  • Thanks 1
Posted

This is what I use and I am pretty sure it was posted by Lee-mac many years ago.

 

(setq plent (entsel "\nPick pline "))
(if plent (setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent))))))
so if you want pt1 pt2
(setq pt1 (car co-ord) pt2 (cadr co-ord))

You can also use the Vl functions getstartpoint & getendpoint

  • Thanks 1
Posted (edited)

Based on your way of processing, here is what I think is what you are after.

(if (and (setq s (car (entsel "\nSelect LWpolyline : ")))
         (= (cdr (assoc 0 (setq bm (entget s)))) "LWPOLYLINE")
    )
  (progn
    (foreach grp bm
      (if (= (car grp) 10)
        (setq pts (cons (cdr grp) pts))
      )
    )
    (entmod (append bm '((370 . 10))))
    (setq p1 (car pts))
    (foreach pt (cdr pts)
      (command "DIMALIGNED" "_non" p1 "_non" pt "@0.35<180")
      (setq p1 pt)
    )
  )
)

 

Edited by Tharwat
  • Thanks 1
Posted (edited)

I see there is consensus on the idea of getting each pair of points by executing 'foreach' twice
In my case, I'm going to insist on getting it in just 1, reducing the executed code as much as possible (or at least that's what I think) and preserving the original code as much as possible.
I suppose there will be differences of opinion on this

 

(defun c:myDimAlign (/ bm pline sublist pt1 pt2)
  (if (and (setq pline (car (entsel "\nSelect any LWpolyline...")))
	   (= (cdr (assoc 0 (setq bm (entget pline)))) "LWPOLYLINE"); Check if the entity type is LWPOLYLINE
      )
    (progn
      (command "_.chprop" pline "" "_lw" "10.00" "")
      (foreach sublist bm
	(if (= (car sublist) 10)	; Look for vertex points (coded as 10).
	  (if	pt2
	    (command "DIMALIGNED" (setq pt1 pt2) (setq pt2 (cdr sublist)) "@5<180")
	    (if pt1
	      (command "DIMALIGNED" pt1 (setq pt2 (cdr sublist)) "@5<180")
	      (setq pt1 (cdr sublist))
	    )
	  )				; end progn
	)				; end if
      )					; end foreach
    )					; end progn
  )
)

 

Edited by GLAVCVS
  • Thanks 1
Posted
  On 2/22/2025 at 11:32 PM, Tharwat said:

Based on your way of processing, here is what I think is what you are after.

(if (and (setq s (car (entsel "\nSelect LWpolyline : ")))
         (= (cdr (assoc 0 (setq bm (entget s)))) "LWPOLYLINE")
    )
  (progn
    (foreach grp bm
      (if (= (car grp) 10)
        (setq pts (cons grp pts))
      )
    )
    (entmod (append bm '((370 . 10))))
    (setq p1 (car pts))
    (foreach pt (cdr pts)
      (command "DIMALIGNED" "_non" p1 "_non" pt "@0.35<180")
      (setq p1 pt)
    )
  )
)

 

Expand  

 

By the way, Tharwat: I can't get your code to work
Could it be my problem?

 

  • Thanks 1
Posted
  On 2/23/2025 at 10:41 AM, GLAVCVS said:

 

By the way, Tharwat: I can't get your code to work
Could it be my problem?

 

Expand  

Nothing special in the codes, but if you are not using the English version then you may need to add the prefixed symbols _. to command name.

  • Thanks 1
Posted
  On 2/23/2025 at 2:01 PM, Tharwat said:

Nothing special in the codes, but if you are not using the English version then you may need to add the prefixed symbols _. to command name.

Expand  

 

No. I've already tried those options.
However, when I checked your message and looked at the code again, I thought I saw that you forgot to remove the 10 code in the first 'foreach'. Maybe that's what it is and when drawing the 'dimensions' it does so at the coordinate x=10. For this reason I didn't see the command display anything on the screen.

I'll check it when I get home

  • Thanks 1
Posted

Yeah you are right, I should have retrieved the coordinates excluding the GC 10.

Codes updated.

  • Thanks 1
Posted

Even though the codes should work but would draw them vertically. 🤓

  • Agree 1
  • Thanks 1
Posted
  On 2/23/2025 at 10:31 AM, GLAVCVS said:

I see there is consensus on the idea of getting each pair of points by executing 'foreach' twice
In my case, I'm going to insist on getting it in just 1, reducing the executed code as much as possible (or at least that's what I think) and preserving the original code as much as possible.
I suppose there will be differences of opinion on this

 

(defun c:myDimAlign (/ bm pline sublist pt1 pt2)
  (if (and (setq pline (car (entsel "\nSelect any LWpolyline...")))
	   (= (cdr (assoc 0 (setq bm (entget pline)))) "LWPOLYLINE"); Check if the entity type is LWPOLYLINE
      )
    (progn
      (command "_.chprop" pline "" "_lw" "10.00" "")
      (foreach sublist bm
	(if (= (car sublist) 10)	; Look for vertex points (coded as 10).
	  (if	pt2
	    (command "DIMALIGNED" (setq pt1 pt2) (setq pt2 (cdr sublist)) "@5<180")
	    (if pt1
	      (command "DIMALIGNED" pt1 (setq pt2 (cdr sublist)) "@5<180")
	      (setq pt1 (cdr sublist))
	    )
	  )				; end progn
	)				; end if
      )					; end foreach
    )					; end progn
  )
)

 

Expand  

 

Only one DIMALIGNED call is required, e.g.:

(defun c:mydimalign ( / e p x )
    (if (and (setq e (car (entsel)))
             (= "LWPOLYLINE" (cdr (assoc 0 (setq x (entget e)))))
        )
        (foreach g x
            (cond
                (   (/= 10 (car g)))
                (   p (command "_.dimaligned" p (setq p (cdr g)) "@5<180"))
                (   (setq p (cdr g)))
            )
        )
    )
    (princ)
)

 

  • Like 2
  • Thanks 1
Posted (edited)

@Lee Mac I can only do one thing with you: take my hat off.

Edited by GLAVCVS

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