乐筑天下

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

[编程交流] 创建闭合多段线

[复制链接]

17

主题

72

帖子

55

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
85
发表于 2022-7-5 18:11:14 | 显示全部楼层 |阅读模式
我试图创建一条闭合多段线,如下所示:
 
 
  1. (defun c:pol (/ pt pt2 ptlist tmp myobj a)
  2. ;;;some random stuff I probably gotta put everytime
  3. (setq thisdrawing
  4.        (vla-get-activedocument
  5.              (vlax-get-acad-object)))
  6. (setq mspace (vla-get-modelspace thisdrawing))
  7. ;;;select 1st point, assigned to variable pt
  8. (setq pt (getpoint "\nSpecify start point: "))
  9. ;;;;assign the first point to a list, in this case ptlist
  10. (setq ptlist (cons pt ptlist))
  11. ;;;select the following points, but these are cycled in variable pt2
  12. (while (setq pt2 (getpoint "\nSpecify next point: " pt2))
  13.    (setq ptlist (cons pt2 ptlist))
  14. )
  15.   ;;;repeat initial point so that the polyline is closed
  16. (setq ptlist (cons pt ptlist))
  17. ;; "dissolve" the points into atoms with append:
  18. (setq ptlist (apply 'append ptlist))
  19. (prin1 ptlist)
  20. )

 
该代码最初将变量“pt”用于第一个点和后面的每个点,并工作。但是因为我想闭合它,我想把第一个点赋给一个排他变量,这样,我只需要在最后重复它,我就得到了一条闭合的多段线。现在我得到错误:错误的参数类型:点:nil
回复

使用道具 举报

17

主题

72

帖子

55

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
85
发表于 2022-7-5 18:30:47 | 显示全部楼层
我不知道为什么第一个例子不起作用,我必须创建一个新变量I来计算while循环的数量,然后通过直接查看列表再次添加列表的最后一个元素(起点)。这样地:
 
 
  1. (defun c:pol (/ pt pt2 ptlist tmp myobj a i)
  2. ;;;some random stuff I probably gotta put everytime
  3. (setq thisdrawing
  4.        (vla-get-activedocument
  5.              (vlax-get-acad-object)))
  6. (setq mspace (vla-get-modelspace thisdrawing))
  7. ;;;select 1st point, assigned to variable pt
  8. (setq pt (getpoint "\nSpecify start point: "))
  9. ;;;;assign the first point to a list, in this case ptlist
  10. (setq ptlist (cons pt ptlist))
  11.   (setq i 0)
  12. ;;;select the following points, this time they stay in pt2 or i will get a error
  13. (while (setq pt (getpoint "\nSpecify next point: " pt))
  14.    (setq i (+ i 1))
  15.    (setq ptlist (cons pt ptlist))
  16. )
  17.   ;;;repeat initial point so that the polyline is closed (by going to the end of the list and adding the point again)
  18. (setq ptlist (cons (nth i ptlist) ptlist))
  19. ;; "dissolve" the points into atoms with append:
  20. (setq ptlist (apply 'append ptlist))
  21. (prin1 ptlist)
  22. (prin1 i)
  23.      (setq
  24.        tmp (vlax-make-safearray
  25.               vlax-vbDouble
  26.               (cons 0 (- (length ptlist) 1))
  27.            )
  28.      )
  29.      (vlax-safearray-fill tmp ptlist)
  30.      (setq myobj (vla-addPolyline mspace tmp))
  31. )

 
下一步:对其进行图案填充
回复

使用道具 举报

95

主题

477

帖子

383

银币

后起之秀

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

铜币
475
发表于 2022-7-5 18:32:29 | 显示全部楼层
您可以在新创建的对象“myobj”上使用hatch命令
  1. (command "._hatch" "_S" myobj "" "")
回复

使用道具 举报

17

主题

72

帖子

55

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
85
发表于 2022-7-5 18:47:58 | 显示全部楼层
这不起作用,因为他想要一个点,而折线没有被选中,但即使如此,我认为一个简单的命令是不够的,我想选择一个模式和比例以及。
 
我试着阅读李·麦克创作的elipse hatch,它看起来更强大、更灵活。这只是我评论他的一段代码
 
  1. (vla-appendouterloop ;means it will associate the boundaries with the hatch
  2.                (setq hat ;hat is the hatch variable
  3.                    (vla-addhatch ;add hatch command
  4.                        (setq spc ;drawing space
  5.                            (vlax-get-property (vla-get-activedocument (vlax-get-acad-object));;; stuff i'm supposed to do everytime, probably change the hatch properties below
  6.                                (if (= 1 (getvar 'cvport))
  7.                                    'paperspace
  8.                                    'modelspace
  9.                                )
  10.                            )
  11.                        )
  12.                        achatchpatterntypepredefined
  13.                        "SOLID"
  14.                        :vlax-true
  15.                        achatchobject
  16.                    )
  17.                )
  18.                (vlax-make-variant   ;;;draws the hatch boundaries (i'll try to replace with the polyline)
  19.                    (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0))
  20.                        (list
  21.                            (vla-addellipse spc
  22.                                (vlax-3D-point (trans cen 1 0))
  23.                                (vlax-3D-point (trans (mapcar '- maj cen) 1 0))
  24.                                rat
  25.                            )
  26.                        )
  27.                    )
  28.                )
  29.            )
  30.            (vla-evaluate hat)
回复

使用道具 举报

95

主题

477

帖子

383

银币

后起之秀

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

铜币
475
发表于 2022-7-5 18:56:26 | 显示全部楼层
您可以通过命令行版本将代码中的变量HPSCALE(用于scale)设置为您想要的,以及HPNAME(用于pattern),您只需将它们放回末尾即可。如果我没弄错的话,你是不是在画一条多段线并在其中进行图案填充,以此来实现图案填充?只是好奇,因为我会在周末研究这段代码并提供帮助。对不起,我还没有测试它,您也可以尝试使用entlast来选择最后一个对象。
回复

使用道具 举报

17

主题

72

帖子

55

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
85
发表于 2022-7-5 19:08:10 | 显示全部楼层
我做到了!谢谢broncos15。
 
  1. (vl-load-com)
  2. (defun c:outerloop1(/ acadObj doc patternName patternType bAssociativity modelSpace hatchObj center radius startAngle endAngle arc line outerLoop pt pt2 ptlist tmp myobj a i)
  3.    ;; This example creates an associative hatch in model space, and
  4.    ;; then creates an outer loop for the hatch.
  5.    (setq acadObj (vlax-get-acad-object))
  6.    (setq doc (vla-get-ActiveDocument acadObj))
  7.    ;; Define the hatch
  8.    (setq patternName "ANSI31"
  9.          patternType 0
  10.          bAssociativity :vlax-true)
  11.    
  12.    ;; Create the associative Hatch object
  13.    (setq modelSpace (vla-get-ModelSpace doc))  
  14.    (setq hatchObj (vla-AddHatch modelSpace patternType patternName bAssociativity acHatchObject))
  15.    ;;;select 1st point, assigned to variable pt
  16. (setq pt (getpoint "\nSpecify start point: "))
  17.    ;;;;assign the first point to a list, in this case ptlist
  18. (setq ptlist (cons pt ptlist))
  19.   (setq i 0)
  20.    ;;;select the following points, but these are cycled in variable pt2
  21. (while (setq pt (getpoint "\nSpecify next point: " pt))
  22.    (setq i (+ i 1))
  23.    (setq ptlist (cons pt ptlist))
  24. )
  25.   ;;;repeat initial point so that the polyline is closed
  26. (setq ptlist (cons (nth i ptlist) ptlist))
  27.   ;; "dissolve" the points into atoms with append:
  28. (setq ptlist (apply 'append ptlist))
  29. (prin1 ptlist)
  30. (prin1 i)
  31.    
  32.    ;; Create the outer loop for the hatch.
  33.    ;; An arc and a line are used to create a closed loop.
  34.      (setq
  35.        tmp (vlax-make-safearray
  36.               vlax-vbDouble
  37.               (cons 0 (- (length ptlist) 1))
  38.            )
  39.      )
  40.   (vlax-safearray-fill tmp ptlist)
  41.   
  42.    (setq poly(vla-AddPolyline modelSpace tmp))
  43.    (setq outerLoop (vlax-make-safearray vlax-vbObject '(0 . 0)))
  44.    (vlax-safearray-put-element outerLoop 0 poly)
  45.       
  46.    ;; Append the outer loop to the hatch object, and display the hatch
  47.    (vla-AppendOuterLoop hatchObj outerLoop)
  48.    (vla-Evaluate hatchObj)
  49. )

 
顺便说一句,忽略注释,我已经对不同代码的元素进行了网格划分
回复

使用道具 举报

95

主题

477

帖子

383

银币

后起之秀

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

铜币
475
发表于 2022-7-5 19:14:57 | 显示全部楼层
别担心。我刚刚测试了你的代码,效果很好。如果我要提出一个建议,为什么不让它“直播”,这样用户就可以看到他们正在孵化什么。您可以通过在用户设置点上运行while语句来实现这一点:
  1. (while (setq ptn
  2.      (getpoint
  3.        ptn
  4.        "\nSpecify next point: "
  5.      )
  6.      )
。在while语句中,绘制pline并立即对其进行图案填充。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-12 20:06 , Processed in 0.356375 second(s), 66 queries .

© 2020-2025 乐筑天下

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