Lisp for Dimensions

Lisp for Dimensions

Anonymous
Not applicable
22,463 Views
13 Replies
Message 1 of 14

Lisp for Dimensions

Anonymous
Not applicable

Hellos Lisp Masters I have a small situation here I have over 500 metal arms and I need to mesuare them so I was thinking on a LISP than can help me make this faster I found a few but they dont really get what I need I am new at lisp and I thinks this is for pros. Each metal arm have screws and I need to measure each screw plus the total leng of the arm can any of you help me with a lisp that can help me with that? Basically each dimension needs to stop I founf a lisp that help me a lot but it stops on each vertice of the polyline.

Thank you !!!

22.JPG

0 Likes
22,464 Views
13 Replies
Replies (13)
Message 2 of 14

marko_ribar
Advisor
Advisor

Here, try this code...

 

(defun c:dimarms ( / *adoc* *error* add_vtx ss sslws ssbls i ent bl p ii lw d lwpdl lwcp plwl lwl par v vl p1 p2 pc )

  (vl-load-com)
  (setq *adoc* (vla-get-activedocument (vlax-get-acad-object)))

  (defun *error* ( msg )
    (vla-endundomark *adoc*)
    (if msg (prompt msg))
    (princ)
  )

  (defun add_vtx ( obj add_pt ent_name / bulg sw ew )
      (vla-GetWidth obj (fix add_pt) 'sw 'ew)
      (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-SetWidth obj (fix add_pt) sw (+ sw (* (- ew sw) (- add_pt (fix add_pt)))))
      (vla-SetWidth obj (1+ (fix add_pt)) (+ sw (* (- ew sw) (- add_pt (fix add_pt)))) ew)
      (vla-update obj)
  )

  (vla-startundomark *adoc*)
  (setq ss (ssget "_X" (list (if (eq (getvar 'cvport) 1) (cons 410 (getvar 'ctab)) (cons 410 "Model")))))
  (setq sslws (ssadd))
  (setq ssbls (ssadd))
  (repeat (setq i (sslength ss))
    (setq ent (ssname ss (setq i (1- i))))
    (cond
      ( (eq (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
        (ssadd ent sslws)
      )
      ( (eq (cdr (assoc 0 (entget ent))) "INSERT")
        (ssadd ent ssbls)
      )
    )
  )
  (if (vl-every '(lambda ( x ) (eq (cdr (assoc 2 (entget x))) (cdr (assoc 2 (entget (ssname ssbls 0))))))
        (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssbls)))
      )
    (progn
      (repeat (setq i (sslength ssbls))
        (setq bl (ssname ssbls (setq i (1- i))))
        (setq p (cdr (assoc 10 (entget bl))))
        (repeat (setq ii (sslength sslws))
          (setq lw (ssname sslws (setq ii (1- ii))))
          (setq d (distance p (vlax-curve-getclosestpointto lw p)))
          (setq lwpdl (cons (cons d lw) lwpdl))
        )
        (setq lwcp (cdar (vl-sort lwpdl '(lambda ( a b ) (< (car a) (car b))))))
        (setq lwpdl nil)
        (setq plwl (cons (list p lwcp) plwl))
      )
      (foreach plw plwl
        (add_vtx (vlax-ename->vla-object (cadr plw)) (vlax-curve-getparamatpoint (cadr plw) (vlax-curve-getclosestpointto (cadr plw) (car plw))) (cadr plw))
        (setq lwl (cons (cadr plw) lwl))
      )
      (setq lwl (acet-list-remove-duplicates lwl nil))
      (foreach lw lwl
        (setq par -1)
        (repeat (fix (1+ (vlax-curve-getendparam lw)))
          (setq v (vlax-curve-getpointatparam lw (setq par (1+ par))))
          (setq vl (cons v vl))
        )
        (setq vl (reverse vl))
        (command "_.DIMALIGNED" (car vl) (last vl) (polar (last vl) (- (angle (car vl) (last vl)) (* 0.5 pi)) (* 50.8 (getvar 'dimtxt))))
        (while (< 1 (length vl))
          (setq p1 (car vl) p2 (cadr vl))
          (setq pc (polar p2 (- (angle p1 p2) (* 0.5 pi)) (* 25.4 (getvar 'dimtxt))))
          (command "_.DIMALIGNED" p1 p2 pc)
          (setq vl (cdr vl))
        )
        (setq vl nil)
      )
    )
    (alert "Different blocks/xrefs detected... Prepare drawing with only single block/xref type you want to dimension along their closest LWPOLYLINE...")
  )
  (*error* nil)
)

 HTH, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 3 of 14

scot-65
Advisor
Advisor
The UCS command has always hidden the "3-point" option which
is helpful either as a "Rotated" or "Aligned" dimension.

We use the "Rotated" inside and along with a customized dimcontinue
style dimensioning and when it is finished, the UCS resets back to world.

For "Aligned", the UCS is set but does not reset automatically at the end.

Command: UCS
Current ucs name: *WORLD*
Specify origin of UCS or [Face/NAmed/OBject/Previous/View/World/X/Y/Z/ZAxis] <World>: 3

Select two points along a object, which represents positive X and [Enter]
for the third and there you have it.

You will notice that each dimension line extension is not locked to each other
using this method as compared to the OOTB dimaligned command.

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 4 of 14

marko_ribar
Advisor
Advisor

@scot-65 wrote:
The UCS command has always hidden the "3-point" option which
is helpful either as a "Rotated" or "Aligned" dimension.

We use the "Rotated" inside and along with a customized dimcontinue
style dimensioning and when it is finished, the UCS resets back to world.

For "Aligned", the UCS is set but does not reset automatically at the end.

Command: UCS
Current ucs name: *WORLD*
Specify origin of UCS or [Face/NAmed/OBject/Previous/View/World/X/Y/Z/ZAxis] <World>: 3

Select two points along a object, which represents positive X and [Enter]
for the third and there you have it.

You will notice that each dimension line extension is not locked to each other
using this method as compared to the OOTB dimaligned command.

???

I don't understand... DIMCONTINUE command works as expected and after applied dimaligned command through lisp routine... As a matter a fact I think that OP just want to automatically dimension what has been drawn inside DWG... And my revision on this is that now it can be applied to any curve entity along which blocks/xrefs are positioned (except XLINEs and RAYs - I haven't included those as you can't dimension their end points - they don't have them at both ends)... So now routine may work no matter wheather entities to dimension are on locked layers as they don't get modified - no vertices additions on them...

 

(defun c:dimarms ( / *error* *adoc* ss sslws ssbls i ent ep bl p ii lw d lwpdl lwcp plwl pswl pslws vl p1 p2 pc )

  (vl-load-com)

  (defun *error* ( msg )
    (vla-endundomark *adoc*)
    (if msg (prompt msg))
    (princ)
  )

  (setq *adoc* (vla-get-activedocument (vlax-get-acad-object)))

  (vla-startundomark *adoc*)
  (setq ss (ssget "_X" (list (if (eq (getvar 'cvport) 1) (cons 410 (getvar 'ctab)) (cons 410 "Model")))))
  (setq sslws (ssadd))
  (setq ssbls (ssadd))
  (repeat (setq i (sslength ss))
    (setq ent (ssname ss (setq i (1- i))))
    (cond
      ( (eq (cdr (assoc 0 (entget ent))) "INSERT")
        (ssadd ent ssbls)
      )
      ( (and (not (vl-catch-all-error-p (setq ep (vl-catch-all-apply 'vlax-curve-getendparam (list ent))))) ep)
        (ssadd ent sslws)
      )
    )
  )
  (if (vl-every '(lambda ( x ) (eq (cdr (assoc 2 (entget x))) (cdr (assoc 2 (entget (ssname ssbls 0))))))
        (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssbls)))
      )
    (progn
      (repeat (setq i (sslength ssbls))
        (setq bl (ssname ssbls (setq i (1- i))))
        (setq p (cdr (assoc 10 (entget bl))))
        (repeat (setq ii (sslength sslws))
          (setq lw (ssname sslws (setq ii (1- ii))))
          (setq d (distance p (vlax-curve-getclosestpointto lw p)))
          (setq lwpdl (cons (cons d lw) lwpdl))
        )
        (setq lwcp (cdar (vl-sort lwpdl '(lambda ( a b ) (< (car a) (car b))))))
        (setq lwpdl nil)
        (setq plwl (cons (list p lwcp) plwl))
      )
      (setq plwl (mapcar '(lambda ( x ) (list (vlax-curve-getclosestpointto (cadr x) (car x)) (cadr x))) plwl))
      (foreach plw1 plwl
        (setq pslw plw1)
        (foreach plw2 (vl-remove plw1 plwl)
          (if (equal (cadr plw1) (cadr plw2))
            (setq pslw (cons (car plw2) pslw))
          )
        )
        (if 
          (not 
            (member (last pslw) (apply 'append pslws))
          )
          (setq pslws (cons pslw pslws))
        )
      )
      (foreach pslw pslws
        (setq vl (reverse (cdr (reverse pslw))))
        (setq vl (vl-sort vl '(lambda ( a b ) (< (vlax-curve-getparamatpoint (last pslw) a) (vlax-curve-getparamatpoint (last pslw) b)))))
        (setq vl (cons (vlax-curve-getstartpoint (last pslw)) vl) vl (reverse (cons (vlax-curve-getendpoint (last pslw)) (reverse vl))))
        (command "_.DIMALIGNED" (car vl) (last vl) (polar (last vl) (- (angle (car vl) (last vl)) (* 0.5 pi)) (* 50.8 (getvar 'dimtxt))))
        (while (< 1 (length vl))
          (setq p1 (car vl) p2 (cadr vl))
          (setq pc (polar p2 (- (angle p1 p2) (* 0.5 pi)) (* 25.4 (getvar 'dimtxt))))
          (command "_.DIMALIGNED" p1 p2 pc)
          (setq vl (cdr vl))
        )
        (setq vl nil)
      )
    )
    (alert "Different blocks/xrefs detected... Prepare drawing with only single block/xref type you want to dimension along their closest CURVE entity...")
  )
  (*error* nil)
)

 

Please if you're satisfied with result this code provides, mark this topic with solution - I just don't see what is an issue that isn't already solved...

Regards, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 5 of 14

Anonymous
Not applicable

Sorry  but its not working for my file the dims have to be on the middle of the block and they are not its ok ill just do the 500 barres myself 😞

0 Likes
Message 6 of 14

scot-65
Advisor
Advisor
Dimcontinue first required a dimension to be created so it can continue.
In other words, two commands.

This was (and still is) the behavior from way back before Release 12. At
that time a routine was created which was only one command, and it was
later enhanced to be directed to the proper layer without additional user
input.

I took that idea and further enhanced the routine to first pause so the UCS
could be set, using the 3-point method, then select all points, [Enter], and
finally specify dimension line location. UCS would revert back to home when
finished.

This would work for the OP since his "metal arms" would be of random
angles and this method does not rely on locked dimension line extensions
(he can select the corner of the arm, then the center of the screws, then
the other corner of the arm).

Apologies for the confusion.

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 7 of 14

Anonymous
Not applicable

Hy,

 

Can anyone help with an automatic lisp for dimensioning a distance, with aligned dimension, between some circles that are paralel with a polyline? Something like in the picture below, from the center of circle perpendicular on the polyline.

 

Thanks!

 

0 Likes
Message 8 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... an automatic lisp for dimensioning a distance, with aligned dimension, between some circles that are paralel with a polyline? ....


See the solution over here.  [It's better not to post the same question in more than one place, for the sake of others who may have the same question in the future, and for the sake of others who may have a solution to offer.  If a solution is provided, others may find the question on the other thread, and not know that there is a solution elsewhere.]

Kent Cooper, AIA
0 Likes
Message 9 of 14

Anonymous
Not applicable

Dear all of autolisp master,

 

  I do not know about autolisp code, so i often seek on web to find the code related my work. And I found a autolisp app almost correct as my requirement but I have a issue with dimensions, i need only dimension lines (DLI) for object, no need Dimension Align (DAL). But It appeared at left side of object after i apply app, bottom, top and right did not apprear. Thank for your help. 

The code i found from cadtips.cadalyst.com

 

;;; CADALYST 11/08  www.cadalyst.com/CADtips 
;;; Tip 3026: DPL.lsp	Automatically Dimension Polylines  (c) 2008 Terry Miller

;-------------------------------------------------------------------------------
; Program Name: DPL - Dimension Polylines
; Created By:   Terry Miller (Email: terrycadd@yahoo.com)
;               (URL: http://web2.airmail.net/terrycad)
; Date Created: 5-20-08
; Function:     Dimensions Polyline shapes
;-------------------------------------------------------------------------------
; Revision History
; Rev  By     Date    Description
;-------------------------------------------------------------------------------
; 1    TM   5-20-08   Initial version
;-------------------------------------------------------------------------------
; c:DPL - Dimensions Polyline
;-------------------------------------------------------------------------------
(defun c:DPL (/ EntName^ EntPick@)
  (setvar "CMDECHO" 0)
  (if (setq EntPick@ (entsel "\nSelect Polyline to dimension: "))
    (if (= (cdr (assoc 0 (entget (car EntPick@)))) "LWPOLYLINE")
      (progn
        (setq EntName^ (cdr (assoc -1 (entget (car EntPick@)))))
        (DimPL EntName^)
      );progn
    );if
  );if
  (if (not EntName^)
    (princ "\nNo Polyline selected.")
  );if
  (princ)
);defun c:DPL
;-------------------------------------------------------------------------------
; DimPL - Function to dimension Polyline
; Arguments: 1
;   EntName^ = Polyline entity name
; Returns: Dimensions Polyline
;-------------------------------------------------------------------------------
(defun DimPL (EntName^ / Bottom@ Clayer$ CW# DiffAng DimPts: DimSpace~ EntList@
  Item LastAng~ LastPt Left@ List@ NW@ Osmode# P0 P1 P2 Pt Pts@ PtsLen Right@ SE@
  Top@ X~ X1~ X1Y1 X1Y2 X1Ys@ X2~ X2Y1 X2Y2 X2Ys@ XPts@ Y~ Y1~ Y1X1 Y1X2 Y1Xs@ Y2~
  Y2X1 Y2X2 Y2Xs@ YPts@)
  ;-----------------------------------------------------------------------------
  (defun DimPts: (Pts@ StartPt EndPt Type$ / Add Num1~ Num2~ Nums1@ Nums2@ P1 P2
    Pt Return@)
    (setq Add t)
    (foreach Pt (member StartPt (append Pts@ Pts@))
      (if Add
        (setq Return@ (append Return@ (list Pt)))
      );if
      (if (equal Pt EndPt)
        (setq Add nil)
      );if
    );foreach
    (foreach Pt Return@
      (if (member Type$ (list "Left" "Right"))
        (setq Nums1@ (append Nums1@ (list (cadr Pt))))
        (setq Nums1@ (append Nums1@ (list (car Pt))))
      );if
    );foreach
    (foreach Num1~ (vl-sort Nums1@ '<)
      (setq Nums2@ nil)
      (foreach Pt Return@
        (if (member Type$ (list "Left" "Right"))
          (if (= (cadr Pt) Num1~)
            (setq Nums2@ (append Nums2@ (list (car Pt))))
          );if
          (if (= (car Pt) Num1~)
            (setq Nums2@ (append Nums2@ (list (cadr Pt))))
          );if
        );if
      );foreach
      (if (member Type$ (list "Left" "Bottom"))
        (setq Nums2@ (vl-sort Nums2@ '<))
        (setq Nums2@ (reverse (vl-sort Nums2@ '<)))
      );if
      (foreach Num2~ (cdr Nums2@)
        (if (member Type$ (list "Left" "Right"))
          (setq Pt (list Num2~ Num1~))
          (setq Pt (list Num1~ Num2~))
        );if
        (setq Return@ (vl-remove Pt Return@))
      );foreach
    );foreach
    (cond
      ((= Type$ "Left")
        (vl-sort Return@ (function (lambda (P1 P2)(< (cadr P1)(cadr P2)))))
      );case
      ((= Type$ "Top")
        (vl-sort Return@ (function (lambda (P1 P2)(< (car P1)(car P2)))))
      );case
      ((= Type$ "Right")
        (vl-sort Return@ (function (lambda (P1 P2)(> (cadr P1)(cadr P2)))))
      );case
      ((= Type$ "Bottom")
        (vl-sort Return@ (function (lambda (P1 P2)(> (car P1)(car P2)))))
      );case
    );cond
  );defun DimPts:
  ;-----------------------------------------------------------------------------
  (setq EntList@ (entget EntName^))
  (if (= (cdr (assoc 0 EntList@)) "LWPOLYLINE")
    (progn
      (foreach List@ EntList@
        (if (= (car List@) 10)
          (if (not (equal (cdr List@) LastPt))
            (progn
              (setq Pts@ (append Pts@ (list (cdr List@))))
              (if (> (length Pts@) 2)
                (if (/= (angle LastPt (cdr List@)) LastAng~) (setq DiffAng t))
              );if
              (if (> (length Pts@) 1)
                (setq LastAng~ (angle LastPt (cdr List@)))
              );if
              (setq LastPt (cdr List@))
            );progn
          );if
        );if
      );foreach
      (if (equal (car Pts@) (last Pts@))
        (setq Pts@ (reverse (cdr (reverse Pts@))))
      );if
      (setq PtsLen (length Pts@))
    );progn
    (exit)
  );if
  (foreach Pt Pts@
    (setq X~ (atof (rtos (car Pt) 2 8))
          Y~ (atof (rtos (cadr Pt) 2 8))
          XPts@ (append XPts@ (list X~))
          YPts@ (append YPts@ (list Y~))
          Pts@ (cdr (append Pts@ (list (list X~ Y~))))
    );setq
  );foreach
  (setq XPts@ (vl-sort XPts@ '<)
        YPts@ (vl-sort YPts@ '<)
        X1~ (car XPts@)
        X2~ (last XPts@)
        Y1~ (car YPts@)
        Y2~ (last YPts@)
  );if
  (foreach Pt Pts@
    (if (= (car Pt) X1~) (setq X1Ys@ (append X1Ys@ (list (cadr Pt)))))
    (if (= (car Pt) X2~) (setq X2Ys@ (append X2Ys@ (list (cadr Pt)))))
    (if (= (cadr Pt) Y1~) (setq Y1Xs@ (append Y1Xs@ (list (car Pt)))))
    (if (= (cadr Pt) Y2~) (setq Y2Xs@ (append Y2Xs@ (list (car Pt)))))
  );foreach
  (setq X1Ys@ (vl-sort X1Ys@ '<)
        X2Ys@ (vl-sort X2Ys@ '<)
        Y1Xs@ (vl-sort Y1Xs@ '<)
        Y2Xs@ (vl-sort Y2Xs@ '<)
        X1Y1 (list X1~ (car X1Ys@))
        X1Y2 (list X1~ (last X1Ys@))
        X2Y1 (list X2~ (car X2Ys@))
        X2Y2 (list X2~ (last X2Ys@))
        Y1X1 (list (car Y1Xs@) Y1~)
        Y1X2 (list (last Y1Xs@) Y1~)
        Y2X1 (list (car Y2Xs@) Y2~)
        Y2X2 (list (last Y2Xs@) Y2~)
        Pts@ (member X1Y1 (append Pts@ Pts@))
  );setq
  (while (> (length Pts@) PtsLen)
    (setq Pts@ (reverse (cdr (reverse Pts@))))
  );while
  (setq SE@ (member X2Y2 Pts@) NW@ Pts@)
  (foreach Item SE@
    (setq NW@ (vl-remove Item NW@))
  );foreach
  (setq SE@ (append SE@ (list X1Y1))
        NW@ (append NW@ (list X2Y2))
        CW# 0
  );setq
  (foreach Pt (list Y2X1 Y2X2)
    (if (member Pt NW@) (setq CW# (1+ CW#)))
    (if (member Pt SE@) (setq CW# (1- CW#)))
  );foreach
  (foreach Pt (list Y1X1 Y1X2)
    (if (member Pt SE@) (setq CW# (1+ CW#)))
    (if (member Pt NW@) (setq CW# (1- CW#)))
  );foreach
  (if (< CW# 0)
    (setq Pts@ (append (list (car Pts@))(reverse (cdr Pts@))))
  );if
  (setq Left@ (DimPts: Pts@ Y1X1 Y2X1 "Left"))
  (setq Top@ (DimPts: Pts@ X1Y2 X2Y2 "Top"))
  (setq Right@ (DimPts: Pts@ Y2X2 Y1X2 "Right"))
  (setq Bottom@ (DimPts: Pts@ X2Y1 X1Y1 "Bottom"))
  ;-----------------------------------------------------------------------------
  (command "UNDO" "BEGIN")
  (setq DimSpace~ (* (getvar "DIMSCALE") (getvar "DIMTXT") 3))
  (setq Osmode# (getvar "OSMODE")) (setvar "OSMODE" 0)
  (setq Clayer$ (getvar "CLAYER"))
  (command "LAYER" "S" (GetDimLayer) "");<--Change to your Dim layer info
  (setq P0 (polar X1Y1 pi (* DimSpace~ 1.5))
        P1 (car Left@)
  );setq
  (foreach P2 (cdr Left@)
    (command "DIM1" "VER" P1 P2 P0 "")
    (setq P1 P2)
  );foreach
  (if (> (length Left@) 2)
    (progn
      (setq P0 (polar P0 pi DimSpace~))
      (command "DIM1" "VER" (car Left@) (last Left@) P0 "")
    );progn
  );if
  (setq P0 (polar Y2X1 (dtr 90) (* DimSpace~ 1.5))
        P1 (car Top@)
  );setq
  (foreach P2 (cdr Top@)
    (command "DIM1" "HOR" P1 P2 P0 "")
    (setq P1 P2)
  );foreach
  (if (> (length Top@) 2)
    (progn
      (setq P0 (polar P0 (dtr 90) DimSpace~))
      (command "DIM1" "HOR" (car Top@) (last Top@) P0 "")
    );progn
  );if
  (setq P0 (polar X2Y2 0 (* DimSpace~ 1.5))
        P1 (car Right@)
  );setq
  (if (and (> (length Right@) 2) DiffAng)
    (foreach P2 (cdr Right@)
      (command "DIM1" "VER" P1 P2 P0 "")
      (setq P1 P2)
    );foreach
  );if
  (setq P0 (polar Y1X2 (dtr 270) (* DimSpace~ 1.5))
        P1 (car Bottom@)
  );setq
  (if (and (> (length Bottom@) 2) DiffAng)
    (foreach P2 (cdr Bottom@)
      (command "DIM1" "HOR" P1 P2 P0 "")
      (setq P1 P2)
    );foreach
  );if
  (setvar "CLAYER" Clayer$)
  (setvar "OSMODE" Osmode#)
  (command "UNDO" "END")
  (princ)
);defun DimPL
;-------------------------------------------------------------------------------
; GetDimLayer - Returns the layer name that's on and has the most dimensions,
; or the current layer name if there's no dimensions.
;-------------------------------------------------------------------------------
(defun GetDimLayer (/ DimLayer$ EntList@ Index# Layer$ LayerInfo@ LayerList@ List@ Num# SS&)
  (setq Layer$ (getvar "CLAYER"))
  (if (setq SS& (ssget "X" '((0 . "DIMENSION"))))
    (progn
      (setq Index# -1)
      (while (< (setq Index# (1+ Index#)) (sslength SS&))
        (setq EntList@ (entget (ssname SS& Index#))
              DimLayer$ (cdr (assoc 8 EntList@))
              LayerInfo@ (tblsearch "LAYER" DimLayer$)
        );setq
        (if (and (= (cdr (assoc 70 LayerInfo@)) 0)(> (cdr (assoc 62 LayerInfo@)) 0))
          (if (assoc DimLayer$ LayerList@)
            (setq Num# (1+ (cdr (assoc DimLayer$ LayerList@)))
                  LayerList@ (subst (cons DimLayer$ Num#) (assoc DimLayer$ LayerList@) LayerList@)
            );setq
            (setq LayerList@ (append LayerList@ (list (cons DimLayer$ 1))))
          );if
        );if
      );while
      (if LayerList@
        (progn
          (setq Layer$ (car (car LayerList@))
                Num# (cdr (car LayerList@))
          );setq
          (foreach List@ (cdr LayerList@)
            (if (> (cdr List@) Num#)
              (setq Layer$ (car List@)
                    Num# (cdr List@)
              );setq
            );if
          );foreach
        );progn
      );if
    );progn
  );if
  Layer$
);defun GetDimLayer
;-------------------------------------------------------------------------------
(princ);End of DPL.lsp

object.png

 

Message 10 of 14

abidcdm
Explorer
Explorer

;;;=====================================================================================
(defun polylinedims (/ *error* dtr rtd getdimlayer dimpl entname entpick)

(vl-load-com)

(defun *error* (msg)
(vl-bt)
(if
(not
(member msg
(list
"Function cancelled"
"console break"
"quit / exit abort"
)
)
)
(princ (strcat "\nError: " msg))
)

(if clayer (setvar "clayer" clayer))
(if osmode (setvar "osmode" osmode))
(if cmdecho (setvar "cmdecho" cmdecho))

(command "_.undo" "_end")

(princ)
)

(defun dtr (angle_en_degrés)(/ (* angle_en_degrés pi) 180.0))

(defun rtd (angle_en_radians)(/ (* angle_en_radians 180.0) pi))

(defun convertpoly (ent / objname coords lst)

(if ent
(progn
(setq objname
(vlax-get
(setq obj (vlax-ename->vla-object ent))
'ObjectName
)
)
(setq coords (vlax-get obj 'Coordinates))
(if (= objname "AcDb2dPolyline")
(repeat (/ (length coords) 3)
(setq lst
(append lst
(list (car coords) (cadr coords))
)
)
(setq coords (cdddr coords))
)
(setq lst coords)
)
)
)

(vla-delete obj)

(vlax-invoke
(vla-get-ModelSpace
(vla-get-ActiveDocument
(vlax-get-acad-object)
)
)
'addLightWeightPolyline
(append
lst
(list (car lst) (cadr lst))
)
)
)

(defun getdimlayer (/ dimlayer entlist index layer layerinfo layerlst lst num sset)
(setq layer (getvar "clayer"))
(if (setq sset (ssget "x" '((0 . "DIMENSION"))))
(progn
(setq index -1)
(while (< (setq index (1+ index)) (sslength sset))
(setq
entlist (entget (ssname sset index))
dimlayer (cdr (assoc 8 entlist))
layerinfo (tblsearch "layer" dimlayer)
)
(if (and (= (cdr (assoc 70 layerinfo)) 0)(> (cdr (assoc 62 layerinfo)) 0))
(if (assoc dimlayer layerlst)
(setq
num (1+ (cdr (assoc dimlayer layerlst)))
layerlst (subst (cons dimlayer num) (assoc dimlayer layerlst) layerlst)
)
(setq layerlst (append layerlst (list (cons dimlayer 1))))
)
)
)
(if Layerlst
(progn
(setq
layer (car (car Layerlst))
num (cdr (car Layerlst))
)
(foreach lst (cdr Layerlst)
(if (> (cdr lst) num)
(setq
layer (car lst)
num (cdr lst)
)
)
)
)
)
)
)
layer
)

(defun dimpl (entname / bottom clayer cw diffang dimpts dimspace entlist item
lastang lastpt left lst nw osmode p0 p1 p2 pt pts ptslen
right se top x x1 x1y1 x1y2 x1ys x2 x2y1 x2y2 x2ys xpts
y y1 y1x1 y1x2 y1xs y2 y2x1 y2x2 y2xs ypts)

(defun dimpts (pts startpt endpt type / add num1 num2 nums1 nums2 p1 p2 pt return)
(setq add t)
(foreach pt (member startpt (append pts pts))
(if add
(setq return (append return (list Pt)))
)
(if (equal pt endpt)
(setq add nil)
)
)
(foreach pt return
(if (member type (list "Left" "Right"))
(setq nums1 (append nums1 (list (cadr pt))))
(setq nums1 (append nums1 (list (car pt))))
)
)
(foreach num1 (vl-sort nums1 '<)
(setq nums2 nil)
(foreach pt return
(if (member type (list "Left" "Right"))
(if (= (cadr pt) num1)
(setq nums2 (append nums2 (list (car pt))))
)
(if (= (car pt) num1)
(setq nums2 (append nums2 (list (cadr pt))))
)
)
)
(if (member type (list "Left" "Bottom"))
(setq nums2 (vl-sort nums2 '<))
(setq nums2 (reverse (vl-sort nums2 '<)))
)
(foreach num2 (cdr nums2)
(if (member type (list "Left" "Right"))
(setq pt (list num2 num1))
(setq pt (list num1 num2))
)
(setq return (vl-remove Pt return))
)
)
(cond
((= type "Left")
(vl-sort return (function (lambda (p1 p2)(< (cadr p1)(cadr p2)))))
)
((= type "Top")
(vl-sort return (function (lambda (p1 p2)(< (car p1)(car p2)))))
)
((= type "Right")
(vl-sort return (function (lambda (p1 p2)(> (cadr p1)(cadr p2)))))
)
((= type "Bottom")
(vl-sort return (function (lambda (p1 p2)(> (car p1)(car p2)))))
)
)
)

(if (= (cdr (assoc 0 (entget entname))) "POLYLINE")
(setq entname (vlax-vla-object->ename (convertpoly entname)))
)

(setq entlist (entget entname))

(foreach lst entlist
(if (= (car lst) 10)
(if (not (equal (cdr lst) lastpt))
(progn
(setq pts (append pts (list (cdr lst))))
(if (> (length pts) 2)
(if (/= (angle lastpt (cdr lst)) lastang) (setq diffang t))
)
(if (> (length pts) 1)
(setq lastang (angle lastpt (cdr lst)))
)
(setq lastpt (cdr lst))
)
)
)
)

(if (equal (car pts) (last pts))
(setq pts (reverse (cdr (reverse pts))))
)
(setq ptslen (length pts))

(foreach pt pts
(setq
x (atof (rtos (car Pt) 2 8))
y (atof (rtos (cadr Pt) 2 8))
xpts (append xpts (list x))
ypts (append ypts (list y))
pts (cdr (append pts (list (list x y))))
)
)
(setq
xpts (vl-sort xpts '<)
ypts (vl-sort ypts '<)
x1 (car xpts)
x2 (last xpts)
y1 (car ypts)
y2 (last ypts)
)
(foreach pt pts
(if (= (car pt) x1) (setq x1ys (append x1ys (list (cadr pt)))))
(if (= (car pt) x2) (setq x2ys (append x2ys (list (cadr pt)))))
(if (= (cadr pt) y1) (setq y1xs (append y1xs (list (car pt)))))
(if (= (cadr pt) y2) (setq y2xs (append y2xs (list (car pt)))))
)
(setq
x1ys (vl-sort x1ys '<)
x2ys (vl-sort x2ys '<)
y1xs (vl-sort y1xs '<)
y2xs (vl-sort y2xs '<)
x1y1 (list x1 (car x1ys))
x1y2 (list x1 (last x1ys))
x2y1 (list x2 (car x2ys))
x2y2 (list x2 (last x2ys))
y1x1 (list (car y1xs) y1)
y1x2 (list (last y1xs) y1)
y2x1 (list (car y2xs) y2)
y2x2 (list (last y2xs) y2)
pts (member x1y1 (append pts pts))
)
(while (> (length pts) PtsLen)
(setq pts (reverse (cdr (reverse pts))))
)
(setq se (member X2Y2 pts) nw pts)
(foreach item se
(setq nw (vl-remove item nw))
)
(setq
se (append se (list X1Y1))
nw (append nw (list X2Y2))
cw 0
)
(foreach pt (list y2x1 y2x2)
(if (member pt nw) (setq cw (1+ cw)))
(if (member pt se) (setq cw (1- cw)))
)
(foreach pt (list y1x1 y1x2)
(if (member pt se) (setq cw (1+ cw)))
(if (member pt nw) (setq cw (1- cw)))
)
(if (< cw 0)(setq pts (append (list (car pts))(reverse (cdr pts)))))

(setq left (dimpts pts y1x1 y2x1 "Left"))
(setq top (dimpts pts x1y2 x2y2 "Top"))
(setq right (dimpts pts y2x2 y1x2 "Right"))
(setq bottom (dimpts pts x2y1 x1y1 "Bottom"))

(command "_.undo" "_begin")
(setq dimspace (* (getvar "dimscale") (getvar "dimtxt") 3))
(setq osmode (getvar "osmode"))
(setvar "osmode" 0)
(setq clayer (getvar "clayer"))
(command "_.layer" "_s" (getdimlayer) "")
(setvar "dimtix" 1)
(setq
p0 (polar x1y1 pi (* dimspace 1.5))
p1 (car left)
)
(foreach p2 (cdr left)
(command "_.dim1" "_ver" p1 p2 p0 "")
(setq p1 p2)
)
(if (> (length left) 2)
(progn
(setq P0 (polar P0 pi dimspace))
(command "_.dim1" "_ver" (car left) (last left) p0 "")
)
)
(setq
p0 (polar y2x1 (dtr 90) (* dimspace 1.5))
P1 (car top)
)
(foreach P2 (cdr top)
(command "_.dim1" "_hor" p1 p2 p0 "")
(setq p1 p2)
)
(if (> (length top) 2)
(progn
(setq p0 (polar p0 (dtr 90) dimspace))
(command "_.dim1" "_hor" (car top) (last top) p0 "")
)
)
(setq
p0 (polar x2y2 0 (* dimspace 1.5))
p1 (car right)
)
(if (and (> (length right) 2) diffang)
(foreach p2 (cdr right)
(command "_.dim1" "_ver" p1 p2 p0 "")
(setq p1 p2)
)
)
(setq
p0 (polar y1x2 (dtr 270) (* dimspace 1.5))
p1 (car bottom)
)
(if (and (> (length bottom) 2) diffang)
(foreach P2 (cdr bottom)
(command "_.dim1" "_hor" p1 p2 p0 "")
(setq P1 P2)
)
)

(setvar "clayer" clayer)
(setvar "osmode" osmode)
(command "_.undo" "_end")

(princ)
)

(setq cmdecho (getvar "cmdecho"))
(setvar "cmdecho" 0)

(if (setq entpick (entsel "\nSelect polyline: "))
(if (wcmatch (cdr (assoc 0 (entget (car entpick)))) "*POLYLINE")
(progn
(setq entname (car entpick))
(dimpl entname)
)
(princ "\nNo polyline selected.")
)
(princ "\nNothing selected.")
)

(setvar "cmdecho" cmdecho)

(princ)
)
;;;=====================================================================================
(defun c:pld ()(polylinedims)(princ))
;;;=====================================================================================
(prompt "\n *** PLINEDIM.LSP loaded. Type 'PLD' to run the program. *** ")
;;;=====================================================================================
(princ)
;;;=====================================================================================

Message 11 of 14

nasriwalid2005
Community Visitor
Community Visitor

Hellos Lisp Masters I have a small situation here I have over 500 metal arms and I need to mesuare them so I was thinking on a LISP than can help me make this faster I found a few but they dont really get what I need I am new at lisp and I thinks this is for pros. Each metal arm have screws and I need to measure each screw plus the total leng of the arm can any of you help me with a lisp that can help me with that? Basically each dimension needs to stop I founf a lisp that help me a lot but it stops on each vertice of the polyline.

Thank you !!!

0 Likes
Message 12 of 14

Kent1Cooper
Consultant
Consultant

Illustrate, please.

Kent Cooper, AIA
0 Likes
Message 13 of 14

rolisonfelipe
Collaborator
Collaborator

Hi, Why can't lsp receive the selection variable from more than one object?

in line 337  - (if (setq entpick (entsel "\nSelect polyline: ")); entsel -> for -> ssget

 

Is it possible to select several polylines by changing a variable and doing the same thing?



 

0 Likes
Message 14 of 14

marko_ribar
Advisor
Advisor

I've created several Layers and dimensioned each of polylines in your *.DWG... For dimensioning I used also several Layers colored with yellow... For lisp I used my recently posted autodim.lsp posted in this link : https://www.cadtutor.net/forum/files/file/54-autodimlsp/ 

 

HTH.

M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes