Cerberus 发表于 2022-7-6 10:51:37

3D螺栓创建例行测试

我终于把脚趾伸进了Lisp程序的水池,这是我第一次尝试
 
首先,我不得不将代码分为两部分,因为帖子中最多有10000个字符,很抱歉。
 
下面的程序应该在两个选定的3D板上“钻”一个clearace孔,每个板的厚度不超过70mm。然后绘制一个3D公制螺母、螺栓和垫圈,将两块板栓接在一起。可用的螺栓尺寸为M4至M30(仅限首选尺寸)。
 
首先,必须在需要钻孔的一个面上,在需要螺栓孔的位置放置一个点。
 
加载后,在命令行上键入:mbolt,然后按照提示操作。
 
所有用于改进或修复错误的输入都将受到极大的赞赏,但是,请注意,对于lisp,我是一个完全的新手,因此我仍然不熟悉所有的命令、约定或语法。
 
好了,开始了!

Cerberus 发表于 2022-7-6 10:55:15

第1部分:
 
;c:m4bolt drills hole in two pieces of steel and creates M4 bolt of appropriate length, nut and washers         ^c^c(if (not c:m4bolt)(load "dcbolt.fas"));m4bolt
;c:m5bolt drills hole in two pieces of steel and creates M5 bolt of appropriate length, nut and washers         ^c^c(if (not c:m5bolt)(load "dcbolt.fas"));m5bolt
;c:m6bolt drills hole in two pieces of steel and creates M6 bolt of appropriate length, nut and washers         ^c^c(if (not c:m6bolt)(load "dcbolt.fas"));m6bolt
;c:m8bolt drills hole in two pieces of steel and creates M8 bolt of appropriate length, nut and washers         ^c^c(if (not c:m8bolt)(load "dcbolt.fas"));m8bolt
;c:m10bolt drills hole in two pieces of steel and creates M10 bolt of appropriate length, nut and washers       ^c^c(if (not c:m10bolt)(load "dcbolt.fas"));m10bolt
;c:m12bolt drills hole in two pieces of steel and creates M12 bolt of appropriate length, nut and washers       ^c^c(if (not c:m12bolt)(load "dcbolt.fas"));m12bolt
;c:m16bolt drills hole in two pieces of steel and creates M16 bolt of appropriate length, nut and washers       ^c^c(if (not c:m16bolt)(load "dcbolt.fas"));m16bolt
;c:m20bolt drills hole in two pieces of steel and creates M20 bolt of appropriate length, nut and washers       ^c^c(if (not c:m20bolt)(load "dcbolt.fas"));m20bolt
;c:m24bolt drills hole in two pieces of steel and creates M24 bolt of appropriate length, nut and washers       ^c^c(if (not c:m24bolt)(load "dcbolt.fas"));m24bolt
;c:m30bolt drills hole in two pieces of steel and creates M30 bolt of appropriate length, nut and washers       ^c^c(if (not c:m30bolt)(load "dcbolt.fas"));m30bolt

(defun c:m4bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 4.00)        ;metric bolt size
(setq holedia 5.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 9.00)        ;washer o/d
(setq washert 0.80)        ;washer thickness
(setq boltaf 3.50)        ;half dim across flats
(setq boltthk 2.90)        ;depth of bolt head
(setq nutthk 3.00)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 0.25)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

(defun c:m5bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 5.00)        ;metric bolt size
(setq holedia 6.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 10.00)        ;washer o/d
(setq washert 1.00)        ;washer thickness
(setq boltaf 4.00)        ;half dim across flats
(setq boltthk 3.60)        ;depth of bolt head
(setq nutthk 4.00)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 0.25)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

(defun c:m6bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 6.00)        ;metric bolt size
(setq holedia 7.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 12.50)        ;washer o/d
(setq washert 1.6)        ;washer thickness
(setq boltaf 5.00)        ;half dim across flats
(setq boltthk 4.00)        ;depth of bolt head
(setq nutthk 5.00)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 0.50)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

(defun c:m8bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 8.00)        ;metric bolt size
(setq holedia 10.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 17.00)        ;washer o/d
(setq washert 1.6)        ;washer thickness
(setq boltaf 6.50)        ;half dim across flats
(setq boltthk 5.50)        ;depth of bolt head
(setq nutthk 6.50)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 0.50)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

(defun c:m10bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 10.00)        ;metric bolt size
(setq holedia 12.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 21.00)        ;washer o/d
(setq washert 2.00)        ;washer thickness
(setq boltaf 8.50)        ;half dim across flats
(setq boltthk 7.00)        ;depth of bolt head
(setq nutthk 8.00)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 0.75)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

(defun c:m12bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 12.00)        ;metric bolt size
(setq holedia 14.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 24.00)        ;washer o/d
(setq washert 2.50)        ;washer thickness
(setq boltaf 9.50)        ;half dim across flats
(setq boltthk 8.00)        ;depth of bolt head
(setq nutthk 10.00)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 1.0)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

(defun c:m16bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 16.00)        ;metric bolt size
(setq holedia 18.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 30.00)        ;washer o/d
(setq washert 3.00)        ;washer thickness
(setq boltaf 12.00)        ;half dim across flats
(setq boltthk 10.00)        ;depth of bolt head
(setq nutthk 13.00)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 1.25)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

(defun c:m20bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 20.00)        ;metric bolt size
(setq holedia 22.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 37.00)        ;washer o/d
(setq washert 3.00)        ;washer thickness
(setq boltaf 15.00)        ;half dim across flats
(setq boltthk 13.00)        ;depth of bolt head
(setq nutthk 16.00)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 1.5)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

(defun c:m24bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 24.00)        ;metric bolt size
(setq holedia 28.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 44.00)        ;washer o/d
(setq washert 4.00)        ;washer thickness
(setq boltaf 18.00)        ;half dim across flats
(setq boltthk 15.00)        ;depth of bolt head
(setq nutthk 19.00)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 1.75)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

(defun c:m30bolt ( / msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
(setq msize 30.00)        ;metric bolt size
(setq holedia 34.00)        ;clearance hole dia
(setq len1 -70.00)        ;max depth of drilled hole
(setq washerd 56.00)        ;washer o/d
(setq washert 4.00)        ;washer thickness
(setq boltaf 23.00)        ;half dim across flats
(setq boltthk 19.00)        ;depth of bolt head
(setq nutthk 24.00)        ;depth of nut
(setq ext1 5.00)        ;min extension past nut face
(setq boltend 2.00)        ;sets chamfer on end of bolt
(dcbolt msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend)
)

;------------------------------------------------------------------------------------------------------------------

(defun dcbolt (msize holedia len1 washerd washert boltaf boltthk nutthk ext1 boltend / cm olderr attrerr *error* exsnaps
       curlayer layername colour linetype curlayer2 p1 objtypa ent1 enta entdataa objtypb ent2 entb entdatab
       cyl3 cyl4 washer1 list1 b1 b2 p2 p3 p4d p4 p5d p5 b3 b4 bolthead p6 p7 p8 len2 len3 len4 len5 len6 len7 len8
       len9 b5 msizer p9d p9 len10 p10 p11 b6 b7 shank bolt list2 cyl5 cyl6 washer2 list3 b9 b10 p12 p13 p14
       b11 b12 p15 p16 len11 p17 b13 b14 b15 b16 b17 nut)

;------------------------------------------------------------------------------------------------------------------

Cerberus 发表于 2022-7-6 10:58:35

第2部分:
 
;set error handler and save current settings
(setq cm (getvar "cmdecho"))
(setvar "cmdecho" 0)
;(textscr)
(setq olderr *error*)
(setq *error* attrerr)
(command "_.undo" "_group")
(setq exsnaps (getvar "osmode"))
(command "_ucs" "na" "d" "existing")
(command "_ucs" "na" "s" "existing")

;save current layer status and create layer "bolting"
(setq curlayer (getvar "clayer"))
(setq layername (strcat curlayer " BOLTING"))
(setq colour "8")
(setq linetype "continuous")
(layerset layername colour linetype)
(setq curlayer2 (getvar "clayer"))

;bolt routine start-----------------------------------------------------------------------------------------------

(setvar "osmode" 127)

;set face ucs to face
(command "ucs" "na" "d" "existing")
(princ "Pick face to be drilled")
(command "ucs" "NA" "S" "existing")
(command "ucs" "f" pause "")

;set hole center
(initget 1)
(setq p1 (getpoint "\nPick hole center point:"))

;move ucs to hole center
(command "_ucs" "_o" p1)

;pick second solid to be drilled
(while (/= objtypa "3DSOLID")
   (setq ent1 (entsel "\nPick solids to be drilled:"))
   (if ent1
   (progn
(setq enta (car ent1))
(setq entdataa (entget enta))
(setq objtypa (cdr (assoc 0 entdataa)))
)
   )
   )

;pick second solid to be drilled
(while (/= objtypb "3DSOLID")
   (setq ent2 (entsel "\nPick solids to be drilled:"))
   (if ent2
   (progn
(setq entb (car ent2))
(setq entdatab (entget entb))
(setq objtypb (cdr (assoc 0 entdatab)))
)
   )
   )

;create cylinders and subtract from selected solids to "drill" hole
(command "_cylinder" "0,0,0" "d" holedia len1)
(setq cyl1 (entlast))
(command "_subtract" ent1 "" cyl1 "")
(command "_cylinder" "0,0,0" "d" holedia len1)
(setq cyl2 (entlast))
(command "_subtract" ent2 "" cyl2 "")

;create washer 1
(command "_cylinder" "0,0,0" "d" washerd washert)
(setq cyl3 (entlast))
(command "_cylinder" "0,0,0" "d" holedia washert)
(setq cyl4 (entlast))
(command "_subtract" cyl3 "" cyl4 "")
(setq washer1 (entlast))

;create bolt head
(command "_osmode" 0)
(setq list1 (list 0 0 washert))
(command "_ucs" "_o" list1)
(command "_polygon" "6" "0,0,0" "c" boltaf)
(setq b1 (entlast))
(command "_extrude" b1 "" boltthk)
(setq b2 (entlast))
(setq p2 (list 0 0 boltthk))
(setq p3 (list boltaf 0 boltthk))
(setq p4d (+ 20 boltaf))
(setq p4 (list p4d 0 boltthk))
(setq p5d (- boltthk 11.55))
(setq p5 (list p4d 0 p5d))
(command "_osmode" 0)
(command "_3dpoly" p3 p4 p5 "c")
(setq b3 (entlast))
(command "_revolve" b3 "" "0,0,10" "0,0,0" "360")
(setq b4 (entlast))
(command "_subtract" b2 "" b4 "")
(setq bolthead (entlast))

;select back face
(setvar "osmode" 4)
(initget 1)
(setq p6 (getpoint "\nSet length of bolt by picking back face hole center:"))
(command "osmode" 0)

;calculate bolt grip length
(setq p7 (list 0 0 0))
(setq len2 (distance p6 p7))
(setq len3 (+ len2 washert))
(setq len4 (+ len3 nutthk))
(setq len5 (/ len4 5))
(setq len6 (fix len5))
(setq len7 (+ 1 len6))
(setq len8 (* 5 len7))
(setq len9 (+ len8 ext1))

;create bolt shank
(command "_osmode" 0)
(command "_ucs" "_y" "180")
(command "_cylinder" "0,0,0" "d" msize len9)
(setq b5 (entlast))
(setq msizer (/ msize 2))
(setq p9d (- msizer boltend))
(setq p9 (list p9d 0 len9))
(setq len10 (- len9 boltend))
(setq p10 (list msizer 0 len10))
(setq p11 (list msizer 0 len9))
(command "_osmode" 0)
(command "_3dpoly" p9 p10 p11 "c")
(setq b6 (entlast))
(command "_revolve" b6 "" "0,0,10" "0,0,0" "360")
(setq b7 (entlast))
(command "_subtract" b5 "" b7 "")
(setq shank (entlast))

;join bolt head to shank
(command "_union" bolthead shank "")
(setq bolt (entlast))

;create washer 2
(setq list2 (list 0 0 len2))
(command "_ucs" "_o" list2)
(command "_osmode" 0)
(command "_cylinder" "0,0,0" "d" washerd washert)
(setq cyl5 (entlast))
(command "_cylinder" "0,0,0" "d" holedia washert)
(setq cyl6 (entlast))
(command "_subtract" cyl5 "" cyl6 "")
(setq washer2 (entlast))

;create nut
(setq list3 (list 0 0 washert))
(command "_ucs" "_o" list3)
(command "_polygon" "6" "0,0,0" "c" boltaf)
(setq b9 (entlast))
(command "_extrude" b9 "" nutthk)
(setq b10 (entlast))
(setq p12 (list boltaf 0 0))
(setq p13 (list p4d 0 0))
(setq p14 (list p4d 0 11.55))
(command "_3dpoly" p12 p13 p14 "c")
(setq b11 (entlast))
(command "_revolve" b11 "" "0,0,10" "0,0,0" "360")
(setq b12 (entlast))
(command "_subtract" b10 "" b12 "")
(setq b13 (entlast))
(setq p15 (list boltaf 0 nutthk))
(setq p16 (list p4d 0 nutthk))
(setq len11 (- nutthk 11.55))
(setq p17 (list p4d 0 len11))
(command "_3dpoly" p15 p16 p17 "c")
(setq b14 (entlast))
(command "_revolve" b14 "" "0,0,10" "0,0,0" "360")
(setq b15 (entlast))
(command "_subtract" b13 "" b15 "")
(setq b16 (entlast))
(command "_cylinder" "0,0,0" "d" msize nutthk)
(setq b17 (entlast))
(command "_subtract" b13 "" b17 "")
(setq nut (entlast))

;bolt routine finishes--------------------------------------------------------------------------------------------

;restore all original settings
(command "_osmode" exsnaps)
(command "_ucs" "na" "r" "existing")
(command "_ucs" "na" "d" "existing")
(command "_layer" "s" curlayer "")
(command "_undo" "_end")
(setq *error* olderr)
(princ)
)

;internal error handler
(defun attrerr (str /)
(if (/= str "Function Cancelled")
   (princ (strcat "\nError: "str))
   )

;end of prog statement
(command "_undo" "_end")
(command "_undo" "1")

;restore old error handler
(setq *error* olderr)
(setvar "cmdecho" cm)
(princ)
)

;sub routines------------------------------------------------------------------------------------------------------

;subroutine for creating layer
(defun layerset (layername colour linetype / )
(if (/= colour nil)
   (command "_layer" "m" layername "c" colour layername "lt" linetype layername "")
   )
)

Cerberus 发表于 2022-7-6 11:03:27

请注意,例程应基于当前层为紧固件创建一个新层。一、 e.如果您在一个名为“钢结构”的层上,则应创建一个名为“钢结构螺栓连接”的新层。
 
我想改进但不知道如何改进的事情:
 
1) 目前,初始孔的设置深度只能为70mm。我认为,如果孔深度可以从您尝试“钻孔”的三维实体的深度中获取,则会更好。这样可以将更厚的物品固定在一起。这可能吗?
 
2) 这个例程只允许你将两个项目连接在一起,有没有办法让它允许选择两个以上的项目?
 
3) 如果由于某种原因,例程中途崩溃,它不会将所有内容恢复到其原始状态(UCS、层等),这真的很烦人。在试验过程中,我最终创建了以下层:
钢结构螺栓连接
钢结构螺栓
钢结构螺栓
钢结构螺栓等。有没有办法退回所有东西并阻止这种情况?
 
还有其他改进意见吗?
 
非常感谢。

Cerberus 发表于 2022-7-6 11:04:42

哦,是的,我差点忘了。代码的第一部分说明了如果要创建工具栏或菜单,需要在命令行上键入什么,以及在按钮中输入什么。

GhostRider 发表于 2022-7-6 11:08:33

太酷了!我不得不尝试一下,只是为了看看它是如何工作的。很酷,现在如果有人可以在螺母和螺栓上创建螺纹。。。。大声笑。。。干得好Cerberus!!哦,顺便说一句,它只创建了1层,0个螺栓。我从一个设置为毫米单位的新图形开始,似乎运行得很协调。这是一个屏幕截图

CAB 发表于 2022-7-6 11:12:59

第一次尝试看起来很不错。
 
我附上了修订版。只是另一种方法,我篡改了你的WHILE语句。
 
我没有测试,所以如果有任何错误,请原谅我。
我只是提供一些变化。我没时间了。
 
见附件。
闩LSP

XBR1 发表于 2022-7-6 11:14:47

有人可以澄清一下如何启动LISP过程,它似乎不适合我,加载好你。。。。。。

Bill Tillman 发表于 2022-7-6 11:19:09

我不是LISP程序员,但我安装并成功运行了它。我可以使用绘制螺栓头的部分,所以我研究了代码,得到了足够多的代码,只是感到困惑。也许有人能帮我,因为我真的很想为我的3D工作画一些细节。
 
正如我在绘制螺栓头的部分所看到的,这家伙:
 
a) 。绘制六边多边形
b) 。将其挤出到所需的厚度
c) 。然后他画了一条我似乎无法理解的三维多段线。他挑了几个要点,但我似乎无法理解其中的逻辑。
d) 。然后他从六边形中减去这条3dpoly线,或者反过来。
 
无论如何,如果有人能在这里提供一些建议,也许我可以使用这种方法来构建我的3D螺母和螺栓。
 
顺便说一句,我按照所有步骤关闭snap等。。。这就是我遇到麻烦的3dpolyline。如果我能把那个部分取下来,这看起来像是一个非常简单的方法来绘制一些漂亮的3D螺栓头。

Cerberus 发表于 2022-7-6 11:22:19

账单
 
该例程围绕螺栓中心旋转该多段线,然后将其从六边形形状中减去。这就是围绕螺栓顶部以及螺母顶部和底部创建倒角的原因。
 
为螺栓头拾取中心点后,它会将UCS原点设置到此点。多边形是使用关于这个新原点的笛卡尔坐标绘制的。例程完成后,UCS应返回其原始位置和方向。
 
希望这有帮助。
 
顺便说一句,自从发布这个命令以来,我稍微改进了这个例程,这样当你在命令执行到一半的时候按escape键时,它就不会尴尬地崩溃。
 
我还在开发一个版本,创建内六角螺钉和平头螺钉。我会尽快张贴它完成。
 
不过,我仍然没有想出如何选择两个以上的板来栓接在一起。
页: [1] 2
查看完整版本: 3D螺栓创建例行测试