乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 39|回复: 19

[编程交流] Handle and Centre Co-ordinate

[复制链接]

2

主题

9

帖子

7

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 06:35:34 | 显示全部楼层 |阅读模式
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.
回复

使用道具 举报

6

主题

249

帖子

247

银币

初来乍到

Rank: 1

铜币
30
发表于 2022-7-6 06:39:59 | 显示全部楼层
@Clare, post the two lisps you found, or links, and we'll see if someone can "mash" the two together for you.
cheers
回复

使用道具 举报

2

主题

9

帖子

7

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 06:43:34 | 显示全部楼层
Gah, as it's being moderated due to links...
 
1 is Lee Mac's GetHand lsp:
 
  1. (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:
 
  1. (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.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 06:45:25 | 显示全部楼层
Hi Clare,
 
Try something like this:
 
  1. [color=GREEN];; Extract Handles and Centroids to CSV  -  Lee Mac 2012[/color]([color=BLUE]defun[/color] c:hcext ( [color=BLUE]/[/color] ad as cn fd fn rg sp )   ([color=BLUE]if[/color]       ([color=BLUE]and[/color]           ([color=BLUE]ssget[/color] '((0 . [color=MAROON]"LWPOLYLINE"[/color]) (-4 . [color=MAROON]"&="[/color]) (70 . 1)))           ([color=BLUE]setq[/color] fn ([color=BLUE]getfiled[/color] [color=MAROON]"Create Output File"[/color] [color=MAROON]""[/color] [color=MAROON]"csv"[/color] 1))       )       ([color=BLUE]progn[/color]           ([color=BLUE]setq[/color] ad ([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color]))                 sp ([color=BLUE]vlax-get-property[/color] ad ([color=BLUE]if[/color] ([color=BLUE]=[/color] 1 ([color=BLUE]getvar[/color] 'cvport)) 'paperspace 'modelspace))                 fd ([color=BLUE]open[/color] fn [color=MAROON]"w"[/color])           )           ([color=BLUE]write-line[/color] [color=MAROON]"Handle,Centroid X,Centroid Y"[/color] fd)           ([color=BLUE]vlax-for[/color] ob ([color=BLUE]setq[/color] as ([color=BLUE]vla-get-activeselectionset[/color] ad))               ([color=BLUE]setq[/color] rg ([color=BLUE]car[/color] ([color=BLUE]vlax-invoke[/color] sp 'addregion ([color=BLUE]list[/color] ob)))                     cn ([color=BLUE]trans[/color] ([color=BLUE]vlax-get[/color] rg 'centroid) 1 0)               )               ([color=BLUE]vla-delete[/color] rg)               ([color=BLUE]write-line[/color] ([color=BLUE]strcat[/color] ([color=BLUE]vla-get-handle[/color] ob) [color=MAROON]","[/color] ([color=BLUE]rtos[/color] ([color=BLUE]car[/color] cn)) [color=MAROON]","[/color] ([color=BLUE]rtos[/color] ([color=BLUE]cadr[/color] cn))) fd)           )           ([color=BLUE]vla-delete[/color] as)           ([color=BLUE]close[/color] fd)       )   )   ([color=BLUE]princ[/color]))([color=BLUE]vl-load-com[/color]) ([color=BLUE]princ[/color])
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2022-7-6 06:48:54 | 显示全部楼层
Something like this?
  1. (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         [color=magenta](print [/color](trans (vlax-get (setq reg (car (vlax-invoke acspc 'addregion (list obj)))) 'Centroid) 1 0)[color=magenta])[/color]       )       (vla-delete reg)      [color=magenta](princ (cdr (assoc 5 (entget (vlax-vla-object->ename obj)))))[/color]     )     (vla-delete acsel)   ) ) (princ))
回复

使用道具 举报

2

主题

9

帖子

7

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 06:52:04 | 显示全部楼层
PERFECT! Thank you so much!
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 06:55:29 | 显示全部楼层
For fun:
 
  1. (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))
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 06:59:10 | 显示全部楼层
 
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.
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-6 07:02:01 | 显示全部楼层
Renderman , one simple wrong in writing the variable name .
 
  1. (if shell       (vlax-release-object shell))
 
Which must be oShell
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 07:06:28 | 显示全部楼层
Thanks, Tharwat!   (... Thanks a lot, fat fingers! )
 
In addition to this correction, I've added support for 3D (Heavy) Polylines as well.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-10 18:27 , Processed in 0.658993 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表