Handle and Centre Co-ordinate
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. @Clare, post the two lisps you found, or links, and we'll see if someone can "mash" the two together for you.
cheers Gah, as it's being moderated due to links...
1 is Lee Mac's GetHand lsp:
(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:
(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. Hi Clare,
Try something like this:
;; 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) Something like this?
(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)) PERFECT! Thank you so much! For fun:
(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"))) ;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))
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. Renderman , one simple wrong in writing the variable name .
(if shell (vlax-release-object shell))
Which must be oShell Thanks, Tharwat! (... Thanks a lot, fat fingers! )
In addition to this correction, I've added support for 3D (Heavy) Polylines as well.
页:
[1]
2