乐筑天下

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

[编程交流] 画一个正方形

[复制链接]

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 18:14:39 | 显示全部楼层
Tharwat和Lee mac,
我没有机会尝试你的常规动作,但稍后我会的。
然而,我设法(以一种可能很奇怪的方式)将我想要的东西编码为代码:
  1. ; First you draw a line (x------x),  and then it draws the square like this:
  2. ;                      *****************************
  3. ;  the drawn square -> *                           *
  4. ;                      *                           *
  5. ;                      *                           *
  6. ;                      *                           *
  7. ;                      x-------------*-------------x <- the drawn line
  8. ;                      *                           *
  9. ;                      *                           *
  10. ;                      *                           *
  11. ;                      *                           *
  12. ;                      *****************************
  13. (defun C:Square-FullSide ( / *error* oldcmech oldosm tmp-line vla-tmp-line end1 end2 midpt line-ang tmp-circle vla-tmp-circle bbox mn mx rec)
  14. (defun *error* ( msg )
  15.        (if oldosm (setvar 'osmode oldosm))
  16.         (if oldcmech (setvar 'cmdecho oldcmech))
  17.         (if (and (= 'ename (type tmp-line)) (entget tmp-line))
  18.                 (entdel tmp-line)
  19.         )
  20.         (if (and (= 'ename (type tmp-circle)) (entget tmp-circle))
  21.                 (entdel tmp-circle)
  22.         )
  23.        (if (not (member msg '("Function cancelled" "quit / exit abort")))
  24.            (princ (strcat "\nError: " msg))
  25.         )
  26.        (princ)
  27. )
  28. (while
  29.         (or
  30.                 (command "_.line" pause pause "")
  31.                 (and (setq tmp-line (entlast)) (eq (cdr (assoc 0 (entget tmp-line))) "LINE") )
  32.         )
  33.         (progn
  34.                 (setq oldcmech (getvar 'cmdecho))
  35.                 (setq oldosm (getvar 'osmode))
  36.                 (setvar 'cmdecho 0)
  37.                 (setvar 'osmode 0)
  38.                
  39.                
  40.                 (setq end1 (cdr (assoc 10 (entget tmp-line))))
  41.                 (setq end2 (cdr (assoc 11 (entget tmp-line))))
  42.                 (setq midpt (mid end1 end2))
  43.                 (setq ang (angle end1 end2))
  44.                 (setq vla-tmp-line (vlax-ename->vla-object tmp-line))
  45.                 (setq line-ang (vla-get-angle vla-tmp-line))
  46.                 (command "_.circle" "2p" end1 end2)
  47.                 (setq tmp-circle (entlast))
  48.                 (setq vla-tmp-circle (vlax-ename->vla-object tmp-circle))
  49.                 (setq bbox (vla-getboundingbox vla-tmp-circle 'mn 'mx))
  50.                 (command "_.rectangle" (trans (vlax-safearray->list mn) 0 1)
  51.                 (trans (vlax-safearray->list mx) 0 1) )
  52.                 (setq rec (entlast))
  53.                 (command "_.rotate" rec "" midpt (angtos line-ang))
  54.                 (vla-delete vla-tmp-circle)
  55.                 (vla-delete vla-tmp-line)
  56.                 (setvar 'osmode oldosm)
  57.                 (setvar 'cmdecho oldcmech)
  58.         );progn
  59. );if
  60. (princ)
  61. )
  62. (defun mid (p1 p2)
  63. (mapcar '(lambda (x1 x2) (/ (+ x1 x2) 2.0)) p1 p2)
  64. )               
  65. ; First you draw a line (x------x),  and then it draws the square like this:
  66. ;                      *****************************
  67. ;  the drawn square -> *                           *
  68. ;                      *                           *
  69. ;                      *                           *
  70. ;                      *                           *
  71. ;                      *             x-------------x <- the drawn line
  72. ;                      *                           *
  73. ;                      *                           *
  74. ;                      *                           *
  75. ;                      *                           *
  76. ;                      *****************************
  77. (defun C:Square-Halfside (/)
  78. (while T
  79.         (command "_.polygon" 4 pause "C" pause)
  80. )
  81. (princ)
  82. )
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

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

铜币
358
发表于 2022-7-5 18:18:22 | 显示全部楼层
这里有一种替代方法:
  1. (defun c:test  (/ p1 p2 ang dis lst)
  2. (if (and (setq p1 (getpoint "\nSpecify first point :"))
  3.           (setq p2 (getpoint "\nNext point :" p1))
  4.           )
  5.    (progn
  6.      (setq p1  (trans p1 1 0)
  7.            p2  (trans p2 1 0)
  8.            ang (angle p1 p2)
  9.            dis (/ (distance p1 p2) 2.)
  10.            )
  11.      (mapcar
  12.        '(lambda (x)
  13.           (setq
  14.             lst (cons (list (polar x (+ ang (* pi 0.5)) dis)
  15.                             (polar x (+ ang (* pi 1.5)) dis)
  16.                             )
  17.                       lst)))
  18.        (list p1 p2)
  19.        )
  20.      (setq lst (apply 'append lst))
  21.      (entmakex (list '(0 . "LWPOLYLINE")
  22.                      '(100 . "AcDbEntity")
  23.                      '(100 . "AcDbPolyline")
  24.                      '(90 . 4)
  25.                      '(70 . 1)
  26.                      (cons 10 (car lst))
  27.                      (cons 10 (cadr lst))
  28.                      (cons 10 (last lst))
  29.                      (cons 10 (caddr lst))
  30.                      )
  31.                )
  32.      )
  33.    )
  34. (princ)
  35. )
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 18:21:18 | 显示全部楼层
我的$0.05对于矩形拾取点,输入L,输入W,输入ang,然后使用双极的第一原理计算cnr点,使W空白,得到一个正方形,如果空白,则角度相同,然后其水平。请看下面的lisp。
 
可能为L拖动一条线,与为W拖动一条线相同,但使用第一条线的中心点。
像其他人一样,他们可能已经完成了,我刚刚添加了前端。

简单。lsp
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

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

铜币
358
发表于 2022-7-5 18:25:05 | 显示全部楼层
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 18:27:29 | 显示全部楼层
Not yet, but by looking at it I can expect how it would work.
Its missing that vector line I need to define the square rotation, try the routine I posted.
Still I have there 4 send commands - and I'm not sure can I avoid them [well maybe some of them with entmake].
And the 4th send command is the rotation issue I asked for in post #8.
EDIT: Well your first example I implemented in the "Square-HalfSide" function.
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

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

铜币
358
发表于 2022-7-5 18:29:37 | 显示全部楼层
You did not mention how you would like to create that rectangle !
 
Do you have a circle and you wan to select it then draw the box or specify a point with radius then draw a circle wrapped with a box .... etc?
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 18:35:34 | 显示全部楼层
Sorry Tharwat,
But for what I was asking for, your first post gave me the needed answer! Then i just decided to do this other method like in the "Square-FullSide" function. Your 2nd example and Lee's are great, I know they require existing circle, but I have no problems at modifying the codes. I don't blame you at anything, just wanted to show the results that might be useful to someone. And yeah, the code I posted is based on what I have learnt from you and Lee in this forum...
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

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

铜币
358
发表于 2022-7-5 18:38:15 | 显示全部楼层
Simply like this would be enough in my opinion:
 
  1. (defun c:test  (/ p1 p2 ang dis lst) (if (and (setq p1 (getpoint "\nSpecify first point :"))          (setq p2 (getpoint "\nNext point :" p1))          )   (progn     (setq p1  (trans p1 1 0)           p2  (trans p2 1 0)           ang (angle p1 p2)           dis (/ (distance p1 p2) 2.)           )     (mapcar       '(lambda (x)          (setq            lst (cons (list (polar x (+ ang (* pi 0.5)) dis)                            (polar x (+ ang (* pi 1.5)) dis)                            )                      lst)))       (list p1 p2)       )     (setq lst (apply 'append lst))     (entmakex (list '(0 . "LWPOLYLINE")                     '(100 . "AcDbEntity")                     '(100 . "AcDbPolyline")                     '(90 . 4)                     '(70 . 1)                     (cons 10 (car lst))                     (cons 10 (cadr lst))                     (cons 10 (last lst))                     (cons 10 (caddr lst))                     )               )     )   ) (princ) )
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 18:40:58 | 显示全部楼层
Here's an alternative method:
  1. (defun c:test ( / a b n p q u v w )   (and(setq p (getpoint "\nSpecify 1st point: "))       (setq q (getpoint "\nSpecify 2nd point: " p))       (setq v (mapcar '/ (mapcar '- q p) '(2 2))             u (list (- (cadr v)) (car v))             w (list (cadr v) (- (car v)))             n (trans '(0 0 1) 1 0 t)       )       (entmake           (vl-list*              '(000 . "LWPOLYLINE")              '(100 . "AcDbEntity")              '(100 . "AcDbPolyline")              '(090 . 4)              '(070 . 1)               (cons 210 n)               (mapcar '(lambda ( a b ) (cons 10 (trans (mapcar '+ a b) 1 n)))                   (list p q q p)                   (list w w u u)               )           )       )   )   (princ))
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 18:43:42 | 显示全部楼层
My $0.05 for a rectang Pick pt, Enter L, Enter W, Enter ang and use 1st principles of a double polar to work out cnr points Make the w blank and you get a square, same with angle if blank then its horizontal. Look at lisp below.
 
Maybe drag a line For L same for W but use centre pt of first line.
Like others their is probably one already done I just added the front end.
184209nq55arvuswqqnscl.jpg
simplepit.lsp
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-13 04:36 , Processed in 0.372379 second(s), 72 queries .

© 2020-2025 乐筑天下

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