Author Topic: help modifying a lisp routine  (Read 2153 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
help modifying a lisp routine
« on: March 11, 2015, 03:27:12 AM »
Hi all
- This code always ignore one rectangle.

- How to add UNDO.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ColumnSsiZes () (c:CSZ))
  2. (defun c:CSZ ( /  )
  3.   (_makefonts '(("SDC-TEXT" . "Arial.ttf")))
  4.   (setq plinecnt 0)
  5.   (setq Mcnvrt 0.1)
  6.   (setq i -1)
  7.    
  8.   (if (and
  9.         (princ "\nSelect Objects")
  10.         (setq p1 (getpoint))
  11.         (setq p2 (getcorner p1 "\nSelect Objects"))
  12.         )
  13.     (progn
  14.       (command "_.zoom" p1 p2)
  15.       (setq ssPL (ssget "_C" p1 p2  (list (cons 0 "POLYLINE"))))
  16.       (if sspl
  17.         (progn
  18.           (while (setq ent (ssname sspl (setq i (1+ i))))
  19.             (command "_.convertpoly" "l" ent "")
  20.             (entupd ent)
  21.             )
  22.           )
  23.         )
  24.       (setq ss (ssget "_C" p1 p2  (list (cons 0 "ARC,*LINE"))))
  25.       (if ss
  26.         (progn
  27.           (command "_.explode" SS)
  28.           (setq peditaccept (getvar 'peditaccept))
  29.           (setvar 'peditaccept 1)
  30.           (ssget "_C" p1 p2  (list (cons 0 "ARC,*LINE")))
  31.           (command "_.pedit" "_M" ss "" "_J" "" "")
  32.           (setvar 'peditaccept peditaccept)
  33.           ))
  34.       (setq ss (ssget "_C" p1 p2))
  35.       (if ss
  36.         (repeat (sslength ss)
  37.           (setq PlineOb (ssname ss plinecnt))
  38.           (setq PlineObj (vlax-ename->vla-object PlineOb))
  39.           (if (= (cdr (assoc 0 (entget PlineOb))) "LWPOLYLINE")
  40.             (progn
  41.               (if (< 4 (cdr (assoc 90 (entget PlineOb))))
  42.               (progn
  43.                 (PSimple PlineObj)
  44.                 (setq PlineObget (entget PlineOb))
  45.                 (if (< 4 (cdr (assoc 90 PlineObget)))
  46.                   (DimPL PlineOb)
  47.                   ) ;if
  48.                 ) ; progn
  49.               ) ;if
  50.             (setq PlineObget (entget PlineOb))
  51.             (if (< 2 (cdr (assoc 90 PlineObget)))
  52.                 (progn
  53.                   (vla-getboundingbox PlineObj 'mn 'mx)
  54.                   (setq mn (vlax-safearray->list mn))
  55.                   (setq mx (vlax-safearray->list mx))
  56.                   (setq pntnX (nth 0 mn))  (setq pntnY (nth 1 mn))
  57.                   (setq pntxX (nth 0 mx))  (setq pntxY (nth 1 mx))
  58.                   (InsText (strcat " " (rtos (* Mcnvrt (- pntxX pntnX)) 2 0) " x " (rtos (* Mcnvrt (- pntxY pntnY)) 2 0)) (list pntxX pntnY 0.0) T )
  59.                   ) ; progn
  60.                 ) ;if
  61.               ) ;progn
  62.               ) ;if
  63.           (if (= (vla-get-ObjectName PlineObj) "AcDbCircle")
  64.             (progn
  65.               (setq CrclRds  (vla-get-Radius PlineObj))
  66.               (setq CrclCntr (vlax-safearray->list (vlax-variant-value (vla-get-Center PlineObj))))
  67.               (InsText (strcat " D= " (rtos (* 2 Mcnvrt CrclRds) 2 0)) CrclCntr T)
  68.               ) ;progn
  69.             ) ; if
  70.           (setq plinecnt (1+ plinecnt))
  71.           ) ;repeat
  72.         (princ "\nNo lines to get Distance")
  73.         ); if
  74.       ) ;progn
  75.     (princ "\nNo lines to get Distance")
  76.     ) ; if
  77.   (command "_.zoom" "_p")
  78.   (setvar "osmode" 0)
  79.   (princ)
  80.   ) ;defun
  81.    
  82. (princ "\n      q_|_|| _\\|| q_|| _\\|"         )
  83. (princ "\n  Type  CSZ  to invoke the lisp "     )
  84.  
  85. ;;local error handler
  86. (defun *error* (msg)
  87.   (if (member msg '("console break" "Function cancelled"))
  88.       (princ)
  89.       (princ (strcat "\nError " (itoa (getvar "ERRNO")) ": " msg))
  90.   );if
  91. ;;restore old error handler
  92.   (setvar "osmode" 0)
  93.   (setq *error* olderr olderr nil)
  94.  
  95.   (princ)
  96. );*lerror*
  97.  
  98. (defun InsText ( strng pt an / )
  99.     (list
  100.       (cons 0  "TEXT")
  101.       (cons 7 "SDC-TEXT")
  102.       (cons 1  strng)
  103.       (cons 10 Pt)
  104.       (cons 40 250)
  105.       (cons 41 0.75)
  106.     )
  107.   )
  108.   (if an (vl-cmdf "CHPROP" "L" "" "Annotative" "Yes" ""))
  109. )
  110.  
  111.  
  112. ;;by VovKa  http://www.theswamp.org/index.php?to...4927#msg384927
  113. (defun _makefonts (listoffonts / doc font fntdir st)            ;; Usage (_makefonts '(("SDC-TEXT" . "Arial.ttf")))
  114.         fntdir (strcat (getenv "windir") "\\Fonts\\"))
  115.   (foreach f listoffonts
  116.     (cond ((or (setq font (findfile (cdr f)))
  117.                (and (setq font (findfile (strcat fntdir (cdr f)))) (findfile font)))
  118.            (setq st (vla-add (vla-get-textstyles doc) (car f)))
  119.            (vlax-put-property st 'fontfile font)
  120.            (vlax-put-property st 'width 1.0)
  121.            (vlax-put-property st 'height 0.0)
  122.            )))
  123.   (princ))
  124. ; END DEFUN - _makefonts
  125.  
  126. ; DimPL - Function to dimension Polyline
  127. ; Created By:   Terry Miller (Email: terrycadd@yahoo.com)
  128. ; Arguments: 1
  129. ;   EntName^ = Polyline entity name
  130. ; Returns: Dimensions Polyline
  131.  
  132. (defun DimPL (EntName^ / Bottom@ Clayer$ CW# DiffAng DimPts: DimSpace~ EntList@
  133.   Item LastAng~ LastPt Left@ List@ NW@ Osmode# P0 P1 P2 Pt Pts@ PtsLen Right@ SE@
  134.   Top@ X~ X1~ X1Y1 X1Y2 X1Ys@ X2~ X2Y1 X2Y2 X2Ys@ XPts@ Y~ Y1~ Y1X1 Y1X2 Y1Xs@ Y2~
  135.   Y2X1 Y2X2 Y2Xs@ YPts@)
  136.  
  137.   (defun DimPts: (Pts@ StartPt EndPt Type$ / Add Num1~ Num2~ Nums1@ Nums2@ P1 P2
  138.     Pt Return@)
  139.     (setq Add t)
  140.     (foreach Pt (member StartPt (append Pts@ Pts@))
  141.       (if Add
  142.         (setq Return@ (append Return@ (list Pt)))
  143.       );if
  144.       (if (equal Pt EndPt)
  145.         (setq Add nil)
  146.       );if
  147.     );foreach
  148.     (foreach Pt Return@
  149.       (if (member Type$ (list "Left" "Right"))
  150.         (setq Nums1@ (append Nums1@ (list (cadr Pt))))
  151.         (setq Nums1@ (append Nums1@ (list (car Pt))))
  152.       );if
  153.     );foreach
  154.     (foreach Num1~ (vl-sort Nums1@ '<)
  155.       (setq Nums2@ nil)
  156.       (foreach Pt Return@
  157.         (if (member Type$ (list "Left" "Right"))
  158.           (if (= (cadr Pt) Num1~)
  159.             (setq Nums2@ (append Nums2@ (list (car Pt))))
  160.           );if
  161.           (if (= (car Pt) Num1~)
  162.             (setq Nums2@ (append Nums2@ (list (cadr Pt))))
  163.           );if
  164.         );if
  165.       );foreach
  166.       (if (member Type$ (list "Left" "Bottom"))
  167.         (setq Nums2@ (vl-sort Nums2@ '<))
  168.         (setq Nums2@ (reverse (vl-sort Nums2@ '<)))
  169.       );if
  170.       (foreach Num2~ (cdr Nums2@)
  171.         (if (member Type$ (list "Left" "Right"))
  172.           (setq Pt (list Num2~ Num1~))
  173.           (setq Pt (list Num1~ Num2~))
  174.         );if
  175.         (setq Return@ (vl-remove Pt Return@))
  176.       );foreach
  177.     );foreach
  178.     (cond
  179.       ((= Type$ "Left")
  180.         (vl-sort Return@ (function (lambda (P1 P2)(< (cadr P1)(cadr P2)))))
  181.       );case
  182.       ((= Type$ "Top")
  183.         (vl-sort Return@ (function (lambda (P1 P2)(< (car P1)(car P2)))))
  184.       );case
  185.       ((= Type$ "Right")
  186.         (vl-sort Return@ (function (lambda (P1 P2)(> (cadr P1)(cadr P2)))))
  187.       );case
  188.       ((= Type$ "Bottom")
  189.         (vl-sort Return@ (function (lambda (P1 P2)(> (car P1)(car P2)))))
  190.       );case
  191.     );cond
  192.   );defun DimPts:
  193.  
  194.   (setq EntList@ (entget EntName^))
  195.   (if (= (cdr (assoc 0 EntList@)) "LWPOLYLINE")
  196.     (progn
  197.       (foreach List@ EntList@
  198.         (if (= (car List@) 10)
  199.           (if (not (equal (cdr List@) LastPt))
  200.             (progn
  201.               (setq Pts@ (append Pts@ (list (cdr List@))))
  202.               (if (> (length Pts@) 2)
  203.                 (if (/= (angle LastPt (cdr List@)) LastAng~) (setq DiffAng t))
  204.               );if
  205.               (if (> (length Pts@) 1)
  206.                 (setq LastAng~ (angle LastPt (cdr List@)))
  207.               );if
  208.               (setq LastPt (cdr List@))
  209.             );progn
  210.           );if
  211.         );if
  212.       );foreach
  213.       (if (equal (car Pts@) (last Pts@))
  214.         (setq Pts@ (reverse (cdr (reverse Pts@))))
  215.       );if
  216.       (setq PtsLen (length Pts@))
  217.     );progn
  218.     (exit)
  219.   );if
  220.   (foreach Pt Pts@
  221.     (setq X~ (atof (rtos (car Pt) 2 8))
  222.           Y~ (atof (rtos (cadr Pt) 2 8))
  223.           XPts@ (append XPts@ (list X~))
  224.           YPts@ (append YPts@ (list Y~))
  225.           Pts@ (cdr (append Pts@ (list (list X~ Y~))))
  226.     );setq
  227.   );foreach
  228.   (setq XPts@ (vl-sort XPts@ '<)
  229.         YPts@ (vl-sort YPts@ '<)
  230.         X1~ (car XPts@)
  231.         X2~ (last XPts@)
  232.         Y1~ (car YPts@)
  233.         Y2~ (last YPts@)
  234.   );if
  235.   (foreach Pt Pts@
  236.     (if (= (car Pt) X1~) (setq X1Ys@ (append X1Ys@ (list (cadr Pt)))))
  237.     (if (= (car Pt) X2~) (setq X2Ys@ (append X2Ys@ (list (cadr Pt)))))
  238.     (if (= (cadr Pt) Y1~) (setq Y1Xs@ (append Y1Xs@ (list (car Pt)))))
  239.     (if (= (cadr Pt) Y2~) (setq Y2Xs@ (append Y2Xs@ (list (car Pt)))))
  240.   );foreach
  241.   (setq X1Ys@ (vl-sort X1Ys@ '<)
  242.         X2Ys@ (vl-sort X2Ys@ '<)
  243.         Y1Xs@ (vl-sort Y1Xs@ '<)
  244.         Y2Xs@ (vl-sort Y2Xs@ '<)
  245.         X1Y1 (list X1~ (car X1Ys@))
  246.         X1Y2 (list X1~ (last X1Ys@))
  247.         X2Y1 (list X2~ (car X2Ys@))
  248.         X2Y2 (list X2~ (last X2Ys@))
  249.         Y1X1 (list (car Y1Xs@) Y1~)
  250.         Y1X2 (list (last Y1Xs@) Y1~)
  251.         Y2X1 (list (car Y2Xs@) Y2~)
  252.         Y2X2 (list (last Y2Xs@) Y2~)
  253.         Pts@ (member X1Y1 (append Pts@ Pts@))
  254.   );setq
  255.   (while (> (length Pts@) PtsLen)
  256.     (setq Pts@ (reverse (cdr (reverse Pts@))))
  257.   );while
  258.   (setq SE@ (member X2Y2 Pts@) NW@ Pts@)
  259.   (foreach Item SE@
  260.     (setq NW@ (vl-remove Item NW@))
  261.   );foreach
  262.   (setq SE@ (append SE@ (list X1Y1))
  263.         NW@ (append NW@ (list X2Y2))
  264.         CW# 0
  265.   );setq
  266.   (foreach Pt (list Y2X1 Y2X2)
  267.     (if (member Pt NW@) (setq CW# (1+ CW#)))
  268.     (if (member Pt SE@) (setq CW# (1- CW#)))
  269.   );foreach
  270.   (foreach Pt (list Y1X1 Y1X2)
  271.     (if (member Pt SE@) (setq CW# (1+ CW#)))
  272.     (if (member Pt NW@) (setq CW# (1- CW#)))
  273.   );foreach
  274.   (if (< CW# 0)
  275.     (setq Pts@ (append (list (car Pts@))(reverse (cdr Pts@))))
  276.   );if
  277.   (setq Left@ (DimPts: Pts@ Y1X1 Y2X1 "Left"))
  278.   (setq Top@ (DimPts: Pts@ X1Y2 X2Y2 "Top"))
  279.   (setq Right@ (DimPts: Pts@ Y2X2 Y1X2 "Right"))
  280.   (setq Bottom@ (DimPts: Pts@ X2Y1 X1Y1 "Bottom"))
  281.  
  282.   (command "UNDO" "BEGIN")
  283.   (setq DimSpace~ 500)
  284.   (setq Osmode# (getvar "OSMODE")) (setvar "OSMODE" 0)
  285.   (setq Clayer$ (getvar "CLAYER"))
  286.   (command "LAYER" "S" (GetDimLayer) "");<--Change to your Dim layer info
  287.   (setq P0 (polar X1Y1 pi (* DimSpace~ 1.5))
  288.         P1 (car Left@)
  289.   );setq
  290.   (foreach P2 (cdr Left@)
  291.     (command "DIM1" "VER" P1 P2 P0 "")
  292.     (setq P1 P2)
  293.   );foreach
  294.   (if (> (length Left@) 2)
  295.     (progn
  296.       (setq P0 (polar P0 pi DimSpace~))
  297.       (command "DIM1" "VER" (car Left@) (last Left@) P0 "")
  298.     );progn
  299.   );if
  300.   (setq P0 (polar Y2X1 (* pi 0.5) (* DimSpace~ 1.5))
  301.         P1 (car Top@)
  302.   );setq
  303.   (foreach P2 (cdr Top@)
  304.     (command "DIM1" "HOR" P1 P2 P0 "")
  305.     (setq P1 P2)
  306.   );foreach
  307.   (if (> (length Top@) 2)
  308.     (progn
  309.       (setq P0 (polar P0 (* pi 0.5) DimSpace~))
  310.       (command "DIM1" "HOR" (car Top@) (last Top@) P0 "")
  311.     );progn
  312.   );if
  313.   (setq P0 (polar X2Y2 0 (* DimSpace~ 1.5))
  314.         P1 (car Right@)
  315.   );setq
  316.   (if (and (> (length Right@) 2) DiffAng)
  317.     (foreach P2 (cdr Right@)
  318.       (command "DIM1" "VER" P1 P2 P0 "")
  319.       (setq P1 P2)
  320.     );foreach
  321.   );if
  322.   (setq P0 (polar Y1X2 (* pi 1.5) (* DimSpace~ 1.5))
  323.         P1 (car Bottom@)
  324.   );setq
  325.   (if (and (> (length Bottom@) 2) DiffAng)
  326.     (foreach P2 (cdr Bottom@)
  327.       (command "DIM1" "HOR" P1 P2 P0 "")
  328.       (setq P1 P2)
  329.     );foreach
  330.   );if
  331.   (setvar "CLAYER" Clayer$)
  332.   (setvar "OSMODE" Osmode#)
  333.   (command "UNDO" "END")
  334.   (princ)
  335. );defun DimPL
  336.  
  337. ;-------------------------------------------------------------------------------
  338. ; GetDimLayer - Returns the layer name that's on and has the most dimensions,
  339. ; or the current layer name if there's no dimensions.
  340. ;-------------------------------------------------------------------------------
  341. (defun GetDimLayer (/ DimLayer$ EntList@ Index# Layer$ LayerInfo@ LayerList@ List@ Num# SS&)
  342.   (setq Layer$ (getvar "CLAYER"))
  343.   (if (setq SS& (ssget "X" '((0 . "DIMENSION"))))
  344.     (progn
  345.       (setq Index# -1)
  346.       (while (< (setq Index# (1+ Index#)) (sslength SS&))
  347.         (setq EntList@ (entget (ssname SS& Index#))
  348.               DimLayer$ (cdr (assoc 8 EntList@))
  349.               LayerInfo@ (tblsearch "LAYER" DimLayer$)
  350.         );setq
  351.         (if (and (= (cdr (assoc 70 LayerInfo@)) 0)(> (cdr (assoc 62 LayerInfo@)) 0))
  352.           (if (assoc DimLayer$ LayerList@)
  353.             (setq Num# (1+ (cdr (assoc DimLayer$ LayerList@)))
  354.                   LayerList@ (subst (cons DimLayer$ Num#) (assoc DimLayer$ LayerList@) LayerList@)
  355.             );setq
  356.             (setq LayerList@ (append LayerList@ (list (cons DimLayer$ 1))))
  357.           );if
  358.         );if
  359.       );while
  360.       (if LayerList@
  361.         (progn
  362.           (setq Layer$ (car (car LayerList@))
  363.                 Num# (cdr (car LayerList@))
  364.           );setq
  365.           (foreach List@ (cdr LayerList@)
  366.             (if (> (cdr List@) Num#)
  367.               (setq Layer$ (car List@)
  368.                     Num# (cdr List@)
  369.               );setq
  370.             );if
  371.           );foreach
  372.         );progn
  373.       );if
  374.     );progn
  375.   );if
  376.   Layer$
  377. );defun GetDimLayer
  378. (princ);End of DPL.lsp
  379.  
  380.  
  381.   ;;;=======================[ PSimple.lsp ]=======================
  382.   ;;; Author: Charles Alan Butler
  383.   ;;; Version:  1.7 Nov. 23, 2007
  384.   ;;; Purpose: To remove unnecessary vertex from a pline
  385.   ;;; Supports arcs and varying widths
  386.   ;;;=============================================================
  387.   ;; This version will remove the first vertex if it is colinear
  388.   ;; and first & last arcs that have the same center
  389.   ;; Open plines that have the same start & end point will be closed
  390.  
  391.   ;;  Argument: et
  392.   ;;    may be an ename, Vla-Object, list of enames or
  393.   ;;    a selection set
  394.   ;;  Returns: a list, (ename message)
  395.   ;;    Massage is number of vertex removed or error message string
  396.   ;;    If a list or selection set a list of lists is returned
  397.  
  398.   (defun PSimple ( et / doc result Tan Replace BulgeCenter RemoveNlst ps1 )
  399.  
  400.     (vl-load-com)
  401.  
  402.     (defun tan ( a ) (/ (sin a) (cos a)))
  403.  
  404.     (defun replace ( lst in itm )
  405.       (setq in (1+ in))
  406.       (mapcar '(lambda (x) (if (zerop (setq in (1- in))) itm x)) lst)
  407.     )
  408.  
  409.     ;;  CAB 11.16.07
  410.     ;;  Remove based on pointer list
  411.     (defun RemoveNlst ( nlst lst )
  412.       (setq in -1)
  413.       (vl-remove-if '(lambda (x) (not (null (vl-position (setq in (1+ in)) nlst)))) lst)
  414.     )
  415.    
  416.     (defun BulgeCenter ( bulge p1 p2 / delta chord radius center )
  417.       (setq delta  (* (atan bulge) 4)
  418.             chord  (distance p1 p2)
  419.             radius (/ chord (sin (/ delta 2)) 2)
  420.             center (polar p1 (+ (angle p1 p2) (/ (- pi delta) 2)) radius)
  421.       )
  422.     )
  423.  
  424.     ;;  Main function to remove vertex
  425.     ;;  ent must be an ename of a LWPolyline
  426.     (defun ps1 ( ent /      aa     cpt    dir    doc    elst   hlst   Remove
  427.                      idx    keep   len    newb   result vlst   x      closed
  428.                      d10    d40    d41    d42    hlst   p1     p2     p3
  429.                      plast  msg )
  430.        
  431.         (setq elst (entget ent)
  432.               msg  "")
  433.         (setq d10 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) elst)))
  434.         (if (> (length d10) 2)
  435.           (progn
  436.             ;;  seperate vertex data
  437.             (setq d40 (vl-remove-if-not '(lambda (x) (= (car x) 40)) elst))
  438.             (setq d41 (vl-remove-if-not '(lambda (x) (= (car x) 41)) elst))
  439.             (setq d42 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 42)) elst)))
  440.             ;;  remove extra vertex from point list
  441.             (setq plast (1- (length d10)))
  442.             (setq p1 0  p2 1  p3 2)
  443.             (if (and (not (setq closed (vlax-curve-isclosed ent)))
  444.                      (equal (car d10) (last d10) 1e-6))
  445.               (progn
  446.                 (setq Closed t ; close the pline
  447.                       elst (subst (cons 70 (1+(cdr(assoc 70 elst))))(assoc 70 elst) elst)
  448.                       msg  " Closed and")
  449.                 (if (and (not(zerop (nth plast d42)))(not(zerop (nth 0 d42))))
  450.                   (setq d10 (reverse(cdr(reverse d10)))
  451.                         d40 (reverse(cdr(reverse d40)))
  452.                         d41 (reverse(cdr(reverse d41)))
  453.                         d42 (reverse(cdr(reverse d42)))
  454.                         plast (1- plast)
  455.                   )
  456.                 )
  457.               )
  458.             )
  459.             (setq idx -1)
  460.             (while (<= (setq idx (1+ idx)) (if closed (+ plast 2) (- plast 2)))
  461.               (cond
  462.                 ((and (or (equal (angle (nth p1 d10) (nth p2 d10))
  463.                                  (angle (nth p2 d10) (nth p3 d10)) 1e-6)
  464.                           (equal (nth p1 d10) (nth p2 d10) 1e-6)
  465.                           (equal (nth p2 d10) (nth p3 d10) 1e-6))
  466.                       (zerop (nth p2 d42))
  467.                       (or (= p1 plast)
  468.                           (zerop (nth p1 d42)))
  469.                  )
  470.                  (setq remove (cons p2 remove)) ; build a pointer list
  471.                  (setq p2 (if (= p2 plast) 0 (1+ p2))
  472.                        p3 (if (= p3 plast) 0 (1+ p3))
  473.                  )
  474.                 )
  475.                 ((and (not (zerop (nth p2 d42)))
  476.                       (or closed (/= p1 plast))
  477.                       (not (zerop (nth p1 d42))) ; got two arcs
  478.                       (equal
  479.                         (setq cpt (BulgeCenter (nth p1 d42) (nth p1 d10) (nth p2 d10)))
  480.                         (BulgeCenter (nth p2 d42) (nth p2 d10) (nth p3 d10))
  481.                         1e-4)
  482.                  )
  483.                  ;;  combine the arcs
  484.                  (setq aa   (+ (* 4 (atan (abs (nth p1 d42))))(* 4 (atan (abs (nth p2 d42)))))
  485.                        newb (tan (/ aa 4.0))
  486.                  )
  487.                  (if (minusp (nth p1 d42))
  488.                    (setq newb (- (abs newb)))
  489.                    (setq newb (abs newb))
  490.                  )
  491.                  (setq remove (cons p2 remove)) ; build a pointer list
  492.                  (setq d42 (replace d42 p1 newb))
  493.                  (setq p2 (if (= p2 plast) 0 (1+ p2))
  494.                        p3 (if (= p3 plast) 0 (1+ p3))
  495.                  )
  496.                 )
  497.                 (t
  498.                  (setq p1 p2
  499.                        p2 (if (= p2 plast) 0 (1+ p2))
  500.                        p3 (if (= p3 plast) 0 (1+ p3))
  501.                  )
  502.                 )
  503.               )
  504.             )
  505.             (if remove
  506.               (progn
  507.                 (setq count (length d10))
  508.                 ;; Rebuild the vertex data with pt, start & end width, bulge
  509.                 (setq d10 (RemoveNlst remove d10)
  510.                       d40 (RemoveNlst remove d40)
  511.                       d41 (RemoveNlst remove d41)
  512.                       d42 (RemoveNlst remove d42)
  513.                 )
  514.                 (setq result (mapcar '(lambda(w x y z) (list(cons 10 w)
  515.                                           x  y
  516.                                           (cons 42 z))) d10 d40 d41 d42)
  517.                 )
  518.                 ;;  rebuild the entity data with new vertex data
  519.                 (setq hlst (vl-remove-if
  520.                              '(lambda (x) (vl-position (car x) '(40 41 42 10))) elst)
  521.                 )
  522.                 (mapcar '(lambda (x) (setq hlst (append hlst x))) result)
  523.                 (setq hlst (subst (cons 90 (length result)) (assoc 90 hlst) hlst))
  524.                 (if (entmod hlst); return ename and number of vertex removed
  525.                   (list ent (strcat msg " Vertex removed " (itoa(- count (length d10)))))
  526.                   (list ent " Error, may be on locked layer.")
  527.                 )
  528.               )
  529.               (list ent "Nothing to remove - no colenier vertex.")
  530.             )
  531.           )
  532.           (list ent "Nothing to do - Only two vertex.")
  533.         )
  534.     )
  535.    
  536.  
  537.     ;;  ========  S T A R T   H E R E  ===========
  538.     (cond
  539.       ((or (=(type et) 'ENAME)
  540.            (and (=(type et) 'VLA-object)
  541.                 (setq et (vlax-vla-object->ename et))))
  542.         (vla-startundomark doc)
  543.         (setq result (ps1 et))
  544.         (vla-endundomark doc)
  545.        )
  546.       ((= (type et) 'PICKSET)
  547.         (vla-startundomark doc)
  548.         (setq result (mapcar '(lambda(x) (ps1 x))
  549.                 (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))))
  550.         (vla-endundomark doc)
  551.       )
  552.       ((listp et)
  553.         (vla-startundomark doc)
  554.         (setq result (mapcar '(lambda(x) (ps1 x)) et))
  555.         (vla-endundomark doc)
  556.       )
  557.       ((setq result "PSimple Error - Wrong Data Type."))
  558.     )
  559.     result
  560.   )

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: help modifying a lisp routine
« Reply #1 on: March 11, 2015, 04:16:02 AM »
This is a continuation of this topic.

HasanCAD, your code is quite peculiar.
Example: you first use the CONVERTPOLY command and then explode all polylines...

To answer your first question:
To avoid skipping entities change line 31 to:
Code: [Select]
(setq ss (ssget ...))
Regarding your second question:
I am sure that if you search the forum you will be able to answer that question yourself. :-D

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: help modifying a lisp routine
« Reply #2 on: March 11, 2015, 04:44:57 AM »
This is a continuation of this topic.
Sorry for duplicate.

HasanCAD, your code is quite peculiar.
Example: you first use the CONVERTPOLY command and then explode all polylines...
Yep I forgot to delete EXPLODE command.
is there any thing else make the code quite peculiar?

To answer your first question:
To avoid skipping entities change line 31 to:
Code: [Select]
(setq ss (ssget ...))
Done and working excellent

Regarding your second question:
I am sure that if you search the forum you will be able to answer that question yourself. :-D
I am quite confusing in using UNDO command.

Thanks roy_043

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: help modifying a lisp routine
« Reply #3 on: March 11, 2015, 09:40:09 AM »
Regarding UNDO:
I now see that the sub routines you employ have their own undo handling. Both DimPL and PSimple try to start and end undo groups. This is problematic as nested undo groups are not possible and you typically want a single undo group for a command function.

So as a first step you must remove these existing lines:
Code: [Select]
(command "UNDO" "BEGIN")
(command "UNDO" "END")
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(vla-startundomark doc) ; 3 times.
(vla-endundomark doc) ; 3 times.

Next you can use this pattern for your command function:
Code: [Select]
(defun c:CSZ ( / doc)
  (vl-load-com)
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (vla-endundomark doc) ; End possible existing undo group.
  (vla-startundomark doc) ; Start a new undo group.
 
  ; Do your stuff.
 
  (vla-endundomark doc) ; End the undo group.
  (princ)
)
« Last Edit: March 11, 2015, 10:11:04 AM by roy_043 »

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: help modifying a lisp routine
« Reply #4 on: March 11, 2015, 10:54:47 AM »
Regarding UNDO:
...
perfect
Thanks roy_043