ahlzl 发表于 2006-12-31 18:34:00

本帖最后由 作者 于 2006-12-31 18:36:53 编辑 <br /><br /> <p>我改了一下程序。现在能实现第8贴中图1的效果。</p><p>Option Explicit<br/>Const PI As Double = 3.1415926535897</p><p>Public Sub Chamfering()<br/>&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim SSet As AcadSelectionSet<br/>&nbsp;&nbsp;&nbsp; If Not IsNull(ThisDrawing.SelectionSets.Item("chamfering")) Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set SSet = ThisDrawing.SelectionSets.Item("chamfering")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SSet.Delete<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Set SSet = ThisDrawing.SelectionSets.Add("chamfering")<br/>&nbsp;&nbsp;&nbsp; ThisDrawing.Utility.Prompt ("选择要斜切的对象...")<br/>&nbsp;&nbsp;&nbsp; SSet.SelectOnScreen<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim ptBase As Variant<br/>&nbsp;&nbsp;&nbsp; ptBase = ThisDrawing.Utility.GetPoint(, "请输入斜切对象的基点:")<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim pt2 As Variant<br/>&nbsp;&nbsp;&nbsp; pt2 = ThisDrawing.Utility.GetPoint(ptBase, "请输入斜切对象的插入点:")<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim angle As Double<br/>&nbsp;&nbsp;&nbsp; angle = ThisDrawing.Utility.GetAngle(ptBase, "请输入倾斜角度:")<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; If Abs((angle / (0.5 * PI)) - Int(angle / (0.5 * PI))) &lt; 0.001 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox ("您输入的角度不合适,无法完成操作!")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim newb As AcadBlock, newbName As String, n As Integer<br/>&nbsp;&nbsp;&nbsp; n = 1<br/>&nbsp;&nbsp;&nbsp; newbName = "ahlzl"<br/>BLOCK2:<br/>&nbsp;&nbsp;&nbsp; For Each newb In ThisDrawing.Blocks<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If newb.Name = newbName Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newbName = "ahlzl" &amp; "_" &amp; CStr(n)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n = n + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GoTo BLOCK2<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Next newb<br/>&nbsp;&nbsp;&nbsp; Set newb = ThisDrawing.Blocks.Add(ptBase, newbName)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim objCollection0() As Object, i As Integer<br/>&nbsp;&nbsp;&nbsp; ReDim objCollection0(SSet.Count - 1) As Object<br/>&nbsp;&nbsp;&nbsp; For i = 0 To SSet.Count - 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set objCollection0(i) = SSet.Item(i)<br/>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim retObjects0 As Variant<br/>&nbsp;&nbsp;&nbsp; retObjects0 = ThisDrawing.CopyObjects(objCollection0, newb)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim a1 As AcadBlockReference<br/>&nbsp;&nbsp;&nbsp; Set a1 = ThisDrawing.ModelSpace.InsertBlock(ptBase, newbName, 1 / Cos(angle), 1, 1, 0)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; a1.Rotate ptBase, DegreeToRadian(-45)<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim strBlkName As String<br/>&nbsp;&nbsp;&nbsp; strBlkName = "CAD倾斜"<br/>&nbsp;&nbsp;&nbsp; n = 1</p><p>&nbsp;&nbsp;&nbsp; Dim blockObj As AcadBlock<br/>BLOCK:<br/>&nbsp;&nbsp;&nbsp; For Each blockObj In ThisDrawing.Blocks<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If blockObj.Name = strBlkName Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strBlkName = "CAD倾斜" &amp; "_" &amp; CStr(n)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n = n + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GoTo BLOCK<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Next blockObj<br/>&nbsp;&nbsp;&nbsp; Set blockObj = ThisDrawing.Blocks.Add(ptBase, strBlkName)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim objCollection(0) As Object<br/>&nbsp;&nbsp;&nbsp; Set objCollection(0) = a1<br/>&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim retObjects As Variant<br/>&nbsp;&nbsp;&nbsp; retObjects = ThisDrawing.CopyObjects(objCollection, blockObj)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim xScale As Double, yScale As Double, zScale As Double, ang As Double<br/>&nbsp;&nbsp;&nbsp; xScale = Cos(PI / 4 - angle / 2) / Cos(DegreeToRadian(45))<br/>&nbsp;&nbsp;&nbsp; yScale = Sin(PI / 4 - angle / 2) / Sin(DegreeToRadian(45))<br/>&nbsp;&nbsp;&nbsp; zScale = 1<br/>&nbsp;&nbsp;&nbsp; ang = PI / 4 + angle / 2<br/>&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim ref1 As AcadBlockReference<br/>&nbsp;&nbsp;&nbsp; Set ref1 = ThisDrawing.ModelSpace.InsertBlock(pt2, strBlkName, xScale, yScale, zScale, ang)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; a1.Delete<br/>&nbsp;&nbsp;&nbsp; SSet.Delete<br/>End Sub</p><p>Private Function DegreeToRadian(angle As Double) As Double<br/>&nbsp;&nbsp;&nbsp; DegreeToRadian = angle * PI / 180<br/>End Function</p><p></p>

byghbcx 发表于 2007-1-8 11:28:00

感谢两位版主提供的程序代码,我试着对块进行转换,但对带属性的块不能生成无名块,还请high兄多指教.

byghbcx 发表于 2007-1-8 11:35:00

<font face="Courier New" color="#ff0000">(<a href="http://www.mjtd.com/object/autolisp/defun.htm" target="_black"><font color="#0000ff">defun</font></a><font color="#000000">
                </font><b><font color="#0000ff">Nmblock</font></b><font color="#000000">
                </font><font color="#ff0000">(</font><font color="#000000">ss pt </font><font color="#ff0000">k </font><a href="http://www.mjtd.com/object/autolisp/47.htm" target="_black"><font color="#0000ff">/</font></a><font color="#000000"> i n num</font><font color="#ff0000">)</font><br/><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/entmake.htm" target="_black"><font color="#0000ff">entmake</font></a><font color="#ff0000">(</font><font color="#000000">list</font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/cons.htm" target="_black"><font color="#0000ff">cons</font></a><font color="#000000">
                </font><font color="#008000">0</font><font color="#000000">
                </font><font color="#ff00ff">"BLOCK"</font><font color="#ff0000">)</font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/cons.htm" target="_black"><font color="#0000ff">cons</font></a><font color="#000000">
                </font><font color="#008000">2</font><font color="#000000">
                </font><font color="#ff00ff">"*u"</font><font color="#ff0000">)</font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/cons.htm" target="_black"><font color="#0000ff">cons</font></a><font color="#000000">
                </font><font color="#008000">70</font><font color="#000000">
                </font><font color="#008000">1</font><font color="#ff0000">)</font><font color="#000000">
                </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/cons.htm" target="_black"><font color="#0000ff">cons</font></a><font color="#000000">
                </font><font color="#008000">10</font><font color="#000000"> pt</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><br/><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/setq.htm" target="_black"><font color="#0000ff">setq</font></a><font color="#000000"> i </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/sslength.htm" target="_black"><font color="#0000ff">sslength</font></a><font color="#000000"> ss</font><font color="#ff0000">)</font><font color="#000000"> n </font><font color="#ff0000">(</font><font color="#008000">-</font><font color="#000000">
                </font><font color="#008000">1</font><font color="#ff0000">)</font><font color="#ff0000">)</font><br/><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/repeat.htm" target="_black"><font color="#0000ff">repeat</font></a><font color="#000000"> i </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/entmake.htm" target="_black"><font color="#0000ff">entmake</font></a><font color="#ff0000">(</font><font color="#000000">cdr </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/entget.htm" target="_black"><font color="#0000ff">entget</font></a><font color="#ff0000">(</font><font color="#000000">ssname ss </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/setq.htm" target="_black"><font color="#0000ff">setq</font></a><font color="#000000"> n </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/1+.htm" target="_black"><font color="#0000ff">1+</font></a><font color="#000000"> n</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><br/><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/setq.htm" target="_black"><font color="#0000ff">setq</font></a><font color="#000000"> num </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/entmake.htm" target="_black"><font color="#0000ff">entmake</font></a><font color="#000000"> '</font><font color="#ff0000">(</font><font color="#ff0000">(</font><font color="#008000">0</font><font color="#000000">
                </font><font color="#008000">.</font><font color="#000000">
                </font><font color="#ff00ff">"ENDBLK"</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><br/><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/entmake.htm" target="_black"><font color="#0000ff">entmake</font></a><font color="#ff0000">(</font><font color="#000000">list</font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/cons.htm" target="_black"><font color="#0000ff">cons</font></a><font color="#000000">
                </font><font color="#008000">0</font><font color="#000000">
                </font><font color="#ff00ff">"INSERT"</font><font color="#ff0000">)</font><font color="#000000">
                </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/cons.htm" target="_black"><font color="#0000ff">cons</font></a><font color="#000000">
                </font><font color="#008000">2</font><font color="#000000"> num</font><font color="#ff0000">)</font><font color="#000000">
                </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/cons.htm" target="_black"><font color="#0000ff">cons</font></a><font color="#000000">
                </font><font color="#008000">10</font><font color="#000000"> pt</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><br/><font color="#000000">
                </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/if.htm" target="_black"><font color="#0000ff">if</font></a><font color="#000000"> k</font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/progn.htm" target="_black"><font color="#0000ff">progn</font></a><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/setq.htm" target="_black"><font color="#0000ff">setq</font></a><font color="#000000"> i </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/sslength.htm" target="_black"><font color="#0000ff">sslength</font></a><font color="#000000"> ss</font><font color="#ff0000">)</font><font color="#000000"> n </font><font color="#ff0000">(</font><font color="#008000">-</font><font color="#000000">
                </font><font color="#008000">1</font><font color="#ff0000">)</font><font color="#ff0000">)</font><br/><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/repeat.htm" target="_black"><font color="#0000ff">repeat</font></a><font color="#000000"> i</font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/entdel.htm" target="_black"><font color="#0000ff">entdel</font></a><font color="#ff0000">(</font><font color="#000000">ssname ss </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/setq.htm" target="_black"><font color="#0000ff">setq</font></a><font color="#000000"> n </font><font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/1+.htm" target="_black"><font color="#0000ff">1+</font></a><font color="#000000"> n</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><font color="#ff0000">)</font><br/><font color="#000000">
                </font><font color="#800080"><span style="BACKGROUND-COLOR: #c0c0c0;"><font color="#800080">; <font color="#ff0000">(</font><a href="http://www.mjtd.com/object/autolisp/command.htm" target="_black"><font color="#0000ff">command</font></a>
                                                <font color="#ff00ff">".erase"</font> ss <font color="#ff00ff">""</font><font color="#ff0000">)</font><br/></font></font><br/></span><font color="#ff0000">)</font></font>

highflybir 发表于 2007-1-8 13:03:00

<p></p><p></p><p>按照ahlzl的两次做块的思路,调整了其中的系数和数值,重新写了一个变换程序,这个程序已经能对任何CAD实体进行变换了。</p>

byghbcx 发表于 2007-1-8 15:06:00

<p>谢谢highflybir兄作出的及时回应,你的程序很好,在第二次匿名块名图块炸开后,有的东西又复原了,如尺寸线,文字等.</p><p>不过用你的Nblock程序对带属性块有作用,加载到我的transblock中可以用.我的程序中还有就是没有清理无用的匿名块.加入反应器后,拉的次数过多,系统会吃不消.</p><p>&nbsp;</p>

changchangyouyu 发表于 2010-9-28 19:48:00

<p>这么好的帖子,怎么就能沉呢,没天理。顶起!</p>

yoyoho 发表于 2010-9-28 21:23:00

<p>感谢分享程序</p>
<p>收藏学习!</p>

cheng5276 发表于 2010-10-6 00:13:00

高手们在华山论剑!太强了!我等丐帮的2袋弟子只能瞠目结舌啊

lrd1861 发表于 2011-7-7 06:06:00

好贴   顶起来

ljpnb 发表于 2011-7-7 06:27:01

确实强大,几位版大厉害,要努力学一下
页: 1 [2] 3
查看完整版本: 【越飞越高讲堂9】如何像photoshop那样---CAD中的平面几何变换及其矩阵