乐筑天下

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

[编程交流] MatchCenInsPoly。lsp

[复制链接]

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 17:51:26 | 显示全部楼层 |阅读模式
大家好!
所以我发现了这件事,现在我正在对代码进行猛烈抨击,
思考它为什么不起作用。
 
185129l2bll32lyo21go1l.jpg
 
让我解释一下:
此例程只期望用户提供一个选择集:
1.所选内容只能包含闭合多段线
2.例程查找每个多段线(质心)的边界框
3.然后选择每个多段线内的对象,并找到其边界框(质心)
4.然后将内部对象从其bbox的中心移动到多边形的中心
 
我已经组装好了,但我不知道离完成还有多远:
  1. ; Method for selecting the polys:
  2. ; Simplier: with "entsel"
  3. ; Harder: with "ssget" method
  4. ; Get individual bounding box for each object (pline)
  5. ; Get individual bounding box for each selection inside each object
  6. ; Possible problems:
  7. ; (princ "\nYou must select polyline")
  8. ; (princ "\nThe polyline must be closed")
  9. ; (princ (strcat "\nThere are " (sslength ss) " selected open polylines, removing them from selection. "))
  10. ; null selection inside a closed poly
  11. ; Current problem, first theres selection "sel-polys", then theres selection "inside-sel":
  12. (defun c:test ( / ent-ply sel-polys inside-sel box-polys box-insidesel reg pl_obj pt_lst_cnt pt_lst )
  13. ; To select plines only
  14. (setq entitytype "*POLYLINE")
  15. (if
  16.         (and (princ "\nSelect slots (closed polylines)") (setq sel-polys ((ssget "_:L")(list (cons 0 entitytype))))
  17.         );and
  18.         ; Repeat this for each polyline in the selection :
  19.         (repeat
  20.                 (setq idx (sslength sel-polys))
  21.                 (setq ent-ply (ssname sel-polys (setq idx (1- idx))))
  22.                 (setq box-polys (LM:ssboundingbox (vlax-ename->vla-object ent-ply )) )
  23.                
  24.                 (cond
  25.                        
  26.                         ; To check if the poly is closed:
  27.                         (  (not
  28.                                 (vlax-property-available-p
  29.                                         (setq obj (vlax-ename->vla-object ent-ply)) 'Area
  30.                                 )
  31.                         )
  32.                         (princ "\n** Invalid Object Selected **")
  33.                         )
  34.                        
  35.                         ; Construct the region to get the centroid for poly:
  36.                         ( (vl-catch-all-error-p
  37.                                 (setq reg
  38.                                         (vl-catch-all-apply 'vlax-invoke
  39.                                                 (list
  40.                                                         (vlax-get-property (vla-get-activedocument (vlax-get-acad-object))
  41.                                                                 (if (= 1 (getvar 'cvport))
  42.                                                                         'paperspace
  43.                                                                         'modelspace
  44.                                                                 )
  45.                                                         )
  46.                                                         'addregion (list (vlax-ename->vla-object ent-ply ))
  47.                                                 )
  48.                                         )
  49.                                 )
  50.                         )
  51.                        
  52.                         (princ "\nUnable to create region from boundary.")
  53.                         )
  54.                        
  55.                         ; Get the objects inside the closed polyline "inside-sel" :
  56.                         ( (setq        pl_obj (vlax-ename->vla-object ent-ply) cc (vla-get-Coordinates pl_obj))
  57.                                 (setq        pt_lst_cnt (/        (length (vlax-safearray->list (vlax-variant-value cc))) 2 ))
  58.                                 (setq cntr 0)
  59.                                 (repeat pt_lst_cnt
  60.                                         (setq pt_lst (cons (vlax-safearray->list (vlax-variant-value (vla-get-Coordinate pl_obj cntr))) pt_lst ) cntr (1+ cntr))
  61.                                 );repeat
  62.                                 (setq inside-sel (ssget "_WP" pt_lst ))
  63.                                        ;(sssetfirst nil inside-sel) ;<- DO I NEED TO DISSELECT "sel-polys" ???
  64.                         )
  65.                        
  66.                         ; Get the "inside-sel" bounding box:                               
  67.                         (   (not (setq box-insidesel (LM:ssboundingbox inside-sel)))
  68.                                 (princ "\nUnable to calculate bounding box for selection.")
  69.                         )
  70.                        
  71.                         ; Check if "inside-sel" is empty
  72.                         ; Move the "inside-sel" objects inside the polyline, from their centroid, to match the poly's centroid:
  73.                         ( (not (null inside-sel))
  74.                                 (vl-cmdf "_.move" inside-sel ""
  75.                                         "_non" (trans (apply 'mapcar (cons '(lambda ( a b ) (/ (+ a b) 2.0)) box-insidesel)) 0 1)
  76.                                         "_non" (vlax-get (car reg) 'centroid)
  77.                                 );vl-cmdf
  78.                                 (princ "\nThe polyline is empty nothing selected ")
  79.                         )
  80.                 );cond
  81.                
  82.         ); end of repeat
  83. );if
  84. (princ)
  85. );defun
  86. ;; Selection Set Bounding Box  -  Lee Mac
  87. ;; Returns a list of the lower-left and upper-right WCS coordinates of a
  88. ;; rectangular frame bounding all objects in a supplied selection set.
  89. ;; s - [sel] Selection set for which to return bounding box
  90. (defun LM:ssboundingbox ( s / a b i m n o )
  91. (repeat (setq i (sslength s))
  92.         (if
  93.                 (and
  94.                         (setq o (vlax-ename->vla-object (ssname s (setq i (1- i)))))
  95.                         (vlax-method-applicable-p o 'getboundingbox)
  96.                         (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list o 'a 'b))))
  97.                 )
  98.                 (setq m (cons (vlax-safearray->list a) m)
  99.                         n (cons (vlax-safearray->list b) n)
  100.                 )
  101.         )
  102. )
  103. (if (and m n)
  104.         (mapcar '(lambda ( a b ) (apply 'mapcar (cons a b))) '(min max) (list m n))
  105. )
  106. )
  107. (vl-load-com) (princ)       

像往常一样,我不在乎学分,所以只要它起作用,请随意获取版权!
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 18:12:05 | 显示全部楼层
以下是一些代码供您考虑:
  1. (defun c:test ( / b e i l m s x )
  2.    (if (setq s (ssget '((0 . "LWPOLYLINE") (-4 . "&=") (70 . 1))))
  3.        (repeat (setq i (sslength s))
  4.            (setq e (ssname s (setq i (1- i)))
  5.                  l (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) (entget e)))
  6.            )
  7.            (if (and (setq m (ssget "_WP" l))
  8.                     (or (not (ssmemb e m)) (ssdel e m))
  9.                     (< 0 (sslength m))
  10.                     (setq b (LM:ssboundingbox m))
  11.                )
  12.                (vl-cmdf "_.move" m ""
  13.                    "_non" (trans (LM:listmid b) 0 1)
  14.                    "_non" (trans (LM:listmid l) e 1)
  15.                )
  16.            )
  17.        )
  18.    )
  19.    (princ)
  20. )
  21. (defun LM:listmid ( lst )
  22.    ((lambda ( n ) (mapcar '(lambda ( x ) (/ x n)) (apply 'mapcar (cons '+ lst)))) (length lst))
  23. )
  24.                
  25. ;; Selection Set Bounding Box  -  Lee Mac
  26. ;; Returns a list of the lower-left and upper-right WCS coordinates of a
  27. ;; rectangular frame bounding all objects in a supplied selection set.
  28. ;; s - [sel] Selection set for which to return bounding box
  29. (defun LM:ssboundingbox ( s / a b i m n o )
  30.    (repeat (setq i (sslength s))
  31.        (if
  32.            (and
  33.                (setq o (vlax-ename->vla-object (ssname s (setq i (1- i)))))
  34.                (vlax-method-applicable-p o 'getboundingbox)
  35.                (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list o 'a 'b))))
  36.            )
  37.            (setq m (cons (vlax-safearray->list a) m)
  38.                  n (cons (vlax-safearray->list b) n)
  39.            )
  40.        )
  41.    )
  42.    (if (and m n)
  43.        (mapcar '(lambda ( a b ) (apply 'mapcar (cons a b))) '(min max) (list m n))
  44.    )
  45. )
  46. (vl-load-com) (princ)

 
请注意,要移动的对象必须在绘图区域中可见,以便ssget“WP”检测到它们。
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 18:24:05 | 显示全部楼层
李,你好,
 
我没想到这个代码会这么短,
也解释了为什么我只懂一半!
 
我只有一个问题:
您是如何确定多段线是否闭合的?
 
由于其“神奇”的性能,这个例程可以成为您网站的一个不错的补充。
我很高兴你帮了我!
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 18:50:12 | 显示全部楼层
 
DXF组70代码是位编码的,因此需要位掩码(“&=”)来确定是否设置了位1。
 
 
也许-虽然,可能有数百个程序,我已经在论坛上分享,但没有添加到我的网站。。。
 
尽管如此,我很高兴能帮上忙。
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 19:01:55 | 显示全部楼层
我真的很感谢你的时间和帮助,
我不知道你在这一点上花了多少精力写程序,在你背后收集了这么多的知识并在论坛上传播。
“谢谢”这个词一定让你很烦。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-12 23:51 , Processed in 0.432494 second(s), 65 queries .

© 2020-2025 乐筑天下

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