+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 32
  1. #1
    Junior Member
    Using
    AutoCAD 2010
    Join Date
    Jul 2012
    Posts
    12

    Exclamation Handle and Centre Co-ordinate Extract LISP

    Registered forum members do not see this ad.

    Hi,

    I have many polylines (all on one layer, all closed).

    I need to extract (in a text or preferably CSV format) the handle of the polyline and the centroid.

    So the end result would look like:

    06HF, 30.5, 50.5
    H32S, 48.2, 30.4

    And so on.

    I can find scripts to extract the handle, and scripts that find the centroid/vertices, but I can't for the life of me find one that does both - and my attempts at mashing one together (I don't know LISP - I need to learn!) have failed.

    My boss has given me 'til Tuesday (Tomorrow) to find a way to do this - or I have to do what I need to do with this manually, which would take FOREVER!

    Thanks,

    Clare.

  2. #2
    Senior Member stevesfr's Avatar
    Computer Details
    stevesfr's Computer Details
    Operating System:
    Vista <ugh>
    Computer:
    HP Pavilion
    Monitor:
    Dell Trinitron
    Using
    AutoCAD 2008
    Join Date
    Jan 2009
    Location
    Central Illinois, USA
    Posts
    270

    Default

    @Clare, post the two lisps you found, or links, and we'll see if someone can "mash" the two together for you.
    cheers

  3. #3
    Junior Member
    Using
    AutoCAD 2010
    Join Date
    Jul 2012
    Posts
    12

    Default

    Gah, as it's being moderated due to links...

    1 is Lee Mac's GetHand lsp:

    Code:
    (defun c:getHand (/ ss file)
      (vl-load-com)
      (if (and (setq ss (ssget))
               (setq file (getfiled "Output File" "" "txt;csv" 9)))
        (progn
          (setq file (open file "a"))
          (mapcar
            (function
              (lambda (x)
                (write-line
                  (cdr (assoc 5 x)) file)))
            (mapcar 'entget
              (vl-remove-if 'listp
                (mapcar 'cadr (ssnamex ss)))))
          (close file))
        (princ "*Cancel*"))
      (princ))
    That gets the handles.

    Next to each handle I need the Centroid co-ordinates. Again Lee Mac has code, but I can't mesh the 2:

    Code:
    (defun c:pc ( / acdoc acspc acsel reg ) (vl-load-com) ;; © Lee Mac 2011
    
      (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object))
            acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'Paperspace 'Modelspace))
      )
      (if (ssget '((0 . "LWPOLYLINE") (-4 . "&=") (70 . 1)))
        (progn
          (vlax-for obj (setq acsel (vla-get-ActiveSelectionSet acdoc))
            (vlax-invoke acspc 'addpoint
              (trans (vlax-get (setq reg (car (vlax-invoke acspc 'addregion (list obj)))) 'Centroid) 1 0)
            )
            (vla-delete reg)
          )
          (vla-delete acsel)
        )
      )
      (princ)
    )
    Any help is appreciated.

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,726

    Default

    Hi Clare,

    Try something like this:

    Code:
    ;; Extract Handles and Centroids to CSV  -  Lee Mac 2012
    
    (defun c:hcext ( / ad as cn fd fn rg sp )
        (if
            (and
                (ssget '((0 . "LWPOLYLINE") (-4 . "&=") (70 . 1)))
                (setq fn (getfiled "Create Output File" "" "csv" 1))
            )
            (progn
                (setq ad (vla-get-activedocument (vlax-get-acad-object))
                      sp (vlax-get-property ad (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))
                      fd (open fn "w")
                )
                (write-line "Handle,Centroid X,Centroid Y" fd)
                (vlax-for ob (setq as (vla-get-activeselectionset ad))
                    (setq rg (car (vlax-invoke sp 'addregion (list ob)))
                          cn (trans (vlax-get rg 'centroid) 1 0)
                    )
                    (vla-delete rg)
                    (write-line (strcat (vla-get-handle ob) "," (rtos (car cn)) "," (rtos (cadr cn))) fd)
                )
                (vla-delete as)
                (close fd)
            )
        )
        (princ)
    )
    (vl-load-com) (princ)
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  5. #5
    Forum Deity MSasu's Avatar
    Discipline
    Construction
    MSasu's Discipline Details
    Occupation
    engineer
    Discipline
    Construction
    Details
    AutoLISP programmer
    Using
    AutoCAD 2013
    Join Date
    Mar 2009
    Location
    Brasov, Romania
    Posts
    3,000

    Default

    Something like this?
    Code:
    (defun c:pc ( / acdoc acspc acsel reg ) (vl-load-com) ;; © Lee Mac 2011
    
      (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object))
            acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'Paperspace 'Modelspace))
      )
      (if (ssget '((0 . "LWPOLYLINE") (-4 . "&=") (70 . 1)))
        (progn
          (vlax-for obj (setq acsel (vla-get-ActiveSelectionSet acdoc))
            (vlax-invoke acspc 'addpoint
              (print (trans (vlax-get (setq reg (car (vlax-invoke acspc 'addregion (list obj)))) 'Centroid) 1 0))
            )
            (vla-delete reg)
           (princ (cdr (assoc 5 (entget (vlax-vla-object->ename obj)))))
          )
          (vla-delete acsel)
        )
      )
      (princ)
    )
    Regards,
    Mircea

    AutoCAD's happy user equation: FILEDIA + PICKADD² + PICKFIRST = 3

  6. #6
    Junior Member
    Using
    AutoCAD 2010
    Join Date
    Jul 2012
    Posts
    12

    Default

    PERFECT! Thank you so much!

  7. #7
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,945

    Default

    For fun:

    Code:
    (defun c:EHC () (c:ExportHandleCentroid))
    (defun c:ExportHandleCentroid  (/ *error* wcs ss path acApp acDoc oSpace
                                    oShell file oRegion centroid)
      ;; RenderMan, 2012
      ;; Special thanks to Lee Mac for demonstrating the region functionality!
      (princ "\rEXPORTHANDLECENTROID ")
      (vl-load-com)
    
      (defun *error*  (msg)
        (if oShell
          (vlax-release-object oShell))
        (if file (close file))
        (if oRegion (vla-delete oRegion))
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat "\n** Error: " msg " ** "))))                 ; Fatal error, display it
        (princ))
    
      (prompt "\nSelect polylines to extract handle and centroid: ")
    
      (if
        (and (setq wcs (= 1 (getvar 'worlducs)))
             (setq ss (ssget '((0 . "LWPOLYLINE") (70 . 1))))
             (setq path
                    (strcat (vl-filename-directory (vl-filename-mktemp))
                            "\\Handles & Centroids.csv"))
             (setq acApp (vlax-get-acad-object))
             (setq acDoc (vla-get-activedocument acApp))
             (setq oSpace (vlax-get-property
                            acDoc
                            (if (= 1 (getvar 'cvport))
                              'paperspace
                              'modelspace)))
             (setq oShell
                    (vla-getinterfaceobject acApp "Shell.Application")))
         (progn
           (setq file (open path "w"))
           (write-line "Handle, X, Y, Z" file)
           (vlax-for oPline (setq ss (vla-get-activeselectionset acDoc))
             (setq oRegion
                    (car (vlax-invoke oSpace 'addregion (list oPline))))
             (setq centroid (trans (vlax-get oRegion 'centroid) 1 0))
             (vla-delete oRegion)
             (write-line
               (strcat (vla-get-handle oPline)
                       ","
                       (rtos (car centroid))
                       ","
                       (rtos (cadr centroid))
                       ","
                       (rtos (caddr centroid)))
               file))
           (setq file (close file))
           (setq oRegion nil)
           (vla-delete ss)
           (vlax-invoke oShell 'open path)
           (*error* nil))
         (cond
           (wcs (*error* "Nothing selected"))
           (ss (*error* "No file specified"))
           ((*error* "The current drawing is not in WCS"))))
      (princ))
    Last edited by BlackBox; 3rd Jul 2012 at 01:03 pm. Reason: Code revised
    "Potential has a shelf life." - Margaret Atwood

  8. #8
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,945

    Default

    Quote Originally Posted by MSasu View Post
    Something like this?
    Code:
    (defun c:pc ( / acdoc acspc acsel reg ) (vl-load-com) ;; © Lee Mac 2011
    
      (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object))
            acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'Paperspace 'Modelspace))
      )
      (if (ssget '((0 . "LWPOLYLINE") (-4 . "&=") (70 . 1)))
        (progn
          (vlax-for obj (setq acsel (vla-get-ActiveSelectionSet acdoc))
            (vlax-invoke acspc 'addpoint
              (print (trans (vlax-get (setq reg (car (vlax-invoke acspc 'addregion (list obj)))) 'Centroid) 1 0))
            )
            (vla-delete reg)
           (princ (cdr (assoc 5 (entget (vlax-vla-object->ename obj)))))
          )
          (vla-delete acsel)
        )
      )
      (princ)
    )
    I'm not sure that the centroid of a Region is the same as that which is calculated via the Polyline's BoundingBox. Also, why not simply extract the Handle Property from the Polyline Object, rather than convert Vla-Object to Ename, and pull from Entity Data? Just curious, my friend.
    "Potential has a shelf life." - Margaret Atwood

  9. #9
    Forum Deity Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draftsman
    Discipline
    Mechanical
    Using
    AutoCAD 2014
    Join Date
    Oct 2009
    Location
    Lives in Abu Dhabi
    Posts
    2,627

    Default

    Renderman , one simple wrong in writing the variable name .

    Code:
    (if shell       (vlax-release-object shell))
    Which must be oShell
    - When aim is being settled in my mind , I have to reach it and get it in hand whatever it costs and wherever it is and will never give up . Tharwat said

  10. #10
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,945

    Default

    Registered forum members do not see this ad.

    Thanks, Tharwat! (... Thanks a lot, fat fingers! )

    In addition to this correction, I've added support for 3D (Heavy) Polylines as well.
    "Potential has a shelf life." - Margaret Atwood

Similar Threads

  1. Lisp for attribute extract from block
    By sadhu in forum AutoLISP, Visual LISP & DCL
    Replies: 16
    Last Post: 15th Nov 2012, 07:23 pm
  2. Centre-line LISP Error
    By Lee Mac in forum AutoLISP, Visual LISP & DCL
    Replies: 13
    Last Post: 26th Nov 2008, 09:21 pm
  3. Lisp routine for ordinate dimensions
    By CraneGuy in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 30th May 2008, 02:54 pm
  4. extract line definition lisp
    By russell84 in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 13th Mar 2008, 04:56 am
  5. Lisp for dimensioning to the centre of a wall?
    By victoreric in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 21st Feb 2005, 02:08 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts