利用VB写的随鼠标移动的图形
<p>Imports Autodesk.AutoCAD.Runtime<br/>Imports Autodesk.AutoCAD.ApplicationServices<br/>Imports Autodesk.AutoCAD.EditorInput<br/>Imports Autodesk.AutoCAD.Geometry<br/>Imports Autodesk.AutoCAD.Interop<br/>Imports Autodesk.AutoCAD.Interop.Common</p><p>Public Class Class1</p><p> <CommandMethod("mtest")> _<br/> Public Sub test()</p><p> Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor<br/> Dim sd As New MotionalMousePoint<br/> AddHandler ed.PointFilter, AddressOf sd.GetMousePoint<br/> Dim pro As PromptPointOptions = New PromptPointOptions("请选择插入点...")<br/> ed.GetPoint(pro)<br/> RemoveHandler ed.PointFilter, AddressOf sd.GetMousePoint</p><p> End Sub<br/>End Class</p><p>Public Class MotionalMousePoint</p><p> Private MousePoint As Point3d = New Point3d(0, 0, 0) '保存当前鼠标位置 <br/> ''动态获取鼠标位置 <br/> Public Sub GetMousePoint(ByVal sender As Object, ByVal e As PointFilterEventArgs)</p><p> MousePoint = e.Context.ComputedPoint<br/> Dim m_ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor</p><p> DelegateTest(MousePoint, AddressOf ObjectMove2Mouse)</p><p> End Sub</p><p> Delegate Sub MathOperator(ByVal pt As Point3d)</p><p> Sub DelegateTest(ByVal pt As Point3d, ByVal op As MathOperator)</p><p> op.Invoke(pt)</p><p> End Sub</p><p> Public Sub ObjectMove2Mouse(ByVal pt As Point3d)</p><p> Dim acadApp As AcadApplication = CType(Application.AcadApplication, AcadApplication)<br/> Dim acadDoc As AcadDocument = acadApp.ActiveDocument<br/> Try<br/> Static blnFirst As Boolean = False<br/> Static blnFst As Boolean = False<br/> Static objTem(2) As AcadObject</p><p><br/> Dim cirObj As AcadCircle<br/> Dim center(2) As Double<br/> Dim radius As Double<br/> Dim lineObj As AcadLine<br/> Dim sPnt(2), ePnt(2) As Double<br/> Dim textObj As AcadText<br/> Dim istPoint(2) As Double</p><p> center(0) = pt(0) + 0 : center(1) = pt(1) + 0 : center(2) = pt(2) + 0 : radius = 50<br/> sPnt(0) = center(0) : sPnt(1) = center(1) : sPnt(2) = 0<br/> ePnt(0) = center(0) + 100 : ePnt(1) = center(1) + 100 : ePnt(2) = 0<br/> istPoint(0) = center(0) : istPoint(1) = center(1) : istPoint(2) = center(2)</p><p> If blnFirst = True Then<br/> For i As Integer = 0 To 2<br/> objTem(i).Delete() '删除上次绘制的对象<br/> Next<br/> cirObj = acadDoc.ModelSpace.AddCircle(center, radius) '创建一个圆对象<br/> cirObj.color = ACAD_COLOR.acRed '将圆的颜色设为红色<br/> lineObj = acadDoc.ModelSpace.AddLine(sPnt, ePnt) '创建一条直线<br/> lineObj.Lineweight = ACAD_LWEIGHT.acLnWt060<br/> textObj = acadDoc.ModelSpace.AddText("哈!成了。", istPoint, 15) '创建单行文字<br/> textObj.color = ACAD_COLOR.acWhite</p><p> objTem(0) = cirObj<br/> objTem(1) = lineObj<br/> objTem(2) = textObj<br/> Else</p><p> cirObj = acadDoc.ModelSpace.AddCircle(center, radius) '创建一个圆对象<br/> cirObj.color = ACAD_COLOR.acRed '将圆的颜色设为红色 <br/> lineObj = acadDoc.ModelSpace.AddLine(sPnt, ePnt) '创建一条直线<br/> lineObj.Lineweight = ACAD_LWEIGHT.acLnWt060<br/> textObj = acadDoc.ModelSpace.AddText("哈!成了。", istPoint, 15) '创建单行文字<br/> textObj.color = ACAD_COLOR.acWhite</p><p> objTem(0) = cirObj<br/> objTem(1) = lineObj<br/> objTem(2) = textObj<br/> blnFirst = True</p><p> End If</p><p> Catch ex As Exception</p><p> End Try</p><p> End Sub</p><p>End Class</p>回复:(scs5999)利用VB写的随鼠标移动的图形
加入鼠标事件方法值得借鉴,但采用不断加入数据库对象、再不断删除的方法来实现动态显示的方法不可取 <p>重新修改为利用块移动</p><p>Imports Autodesk.AutoCAD.Runtime<br/>Imports Autodesk.AutoCAD.ApplicationServices<br/>Imports Autodesk.AutoCAD.EditorInput<br/>Imports Autodesk.AutoCAD.Geometry<br/>Imports Autodesk.AutoCAD.Interop<br/>Imports Autodesk.AutoCAD.Interop.Common</p><p>Public Class Class1</p><p> <CommandMethod("mtest")> _<br/> Public Sub test()</p><p> Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor<br/> Dim sd As New MotionalMousePoint<br/> AddHandler ed.PointFilter, AddressOf sd.GetMousePoint<br/> Dim pro As PromptPointOptions = New PromptPointOptions("请选择插入点...")<br/> ed.GetPoint(pro)</p><p> RemoveHandler ed.PointFilter, AddressOf sd.GetMousePoint</p><p> End Sub<br/>End Class</p><p>Public Class MotionalMousePoint</p><p> Private MousePoint As Point3d = New Point3d(0, 0, 0) '保存当前鼠标位置 <br/> ''动态获取鼠标位置 <br/> Public Sub GetMousePoint(ByVal sender As Object, ByVal e As PointFilterEventArgs)</p><p> MousePoint = e.Context.ComputedPoint<br/> Dim m_ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor</p><p> DelegateTest(MousePoint, AddressOf Move2Mouse)</p><p> End Sub</p><p> Delegate Sub MathOperator(ByVal pt As Point3d)</p><p> Sub DelegateTest(ByVal pt As Point3d, ByVal op As MathOperator)<br/> op.Invoke(pt)<br/> End Sub</p><p> Public Sub Move2Mouse(ByVal pt As Point3d)</p><p> Dim acadApp As AcadApplication = CType(Application.AcadApplication, AcadApplication)<br/> Dim acaddoc As AcadDocument = acadApp.ActiveDocument</p><p> Try<br/> Static blnFirst As Boolean = False<br/> Static basePoint As Object</p><p> Static blkRefObj As AcadBlockReference<br/> Dim insertPnt(2) As Double</p><p> insertPnt(0) = pt(0) + 0 : insertPnt(1) = pt(1) + 0 : insertPnt(2) = pt(2) + 0 '指定模型空间的插入点</p><p> Dim blkObject As AcadBlock<br/> Dim blkName As String = ""<br/> Dim blk As New CreateBlock</p><p> For Each blkObject In acaddoc.Blocks()<br/> If blkObject.Name = "TestBlock1" And blnFirst = False Then<br/> blkName = blkObject.Name<br/> blkRefObj = acaddoc.ModelSpace.InsertBlock(insertPnt, blkName, 1.0#, 1.0#, 1.0#, 0.0#) '插入图块<br/> basePoint = blkRefObj.InsertionPoint<br/> blnFirst = True<br/> Exit For<br/> End If<br/> Next</p><p> If blnFirst = True Then<br/> blkRefObj.Move(basePoint, insertPnt)<br/> basePoint = blkRefObj.InsertionPoint<br/> Else<br/> blkRefObj = blk.CreateBlock()<br/> blkRefObj.Delete()<br/> 'blkRefObj.Update()<br/> 'acaddoc.Regen(AcRegenType.acActiveViewport)<br/> End If<br/> Catch ex As Exception<br/> MsgBox(ex.Message)<br/> End Try<br/> End Sub</p><p>End Class</p><p>Public Class CreateBlock</p><p> Public Function CreateBlock() As AcadBlockReference<br/> Dim acadApp As AcadApplication = CType(Application.AcadApplication, AcadApplication)<br/> Dim acaddoc As AcadDocument = acadApp.ActiveDocument</p><p> Dim blkObj As AcadBlock<br/> Dim insPnt(2) As Double<br/> Dim blkRefObj As AcadBlockReference<br/> Dim insertPnt(2) As Double</p><p> insPnt(0) = 0 : insPnt(1) = 0 : insPnt(2) = 0<br/> insertPnt(0) = 0 : insertPnt(1) = 0 : insertPnt(2) = 0 '指定模型空间的插入点</p><p> blkObj = acaddoc.Blocks.Add(insPnt, "TestBlock1") '在Blocks集合中创建名为TestBlock1的块对象</p><p> '本段代码将在TestBlock1块对象中创建2个图元对象<br/> Dim cirObj As AcadCircle<br/> Dim center(0 To 2) As Double<br/> Dim radius As Double<br/> center(0) = 0 : center(1) = 0 : center(2) = 0 : radius = 38</p><p> Dim lineObj As AcadLine<br/> Dim sPnt(2), ePnt(2) As Double<br/> sPnt(0) = center(0) : sPnt(1) = center(1) : sPnt(2) = 0<br/> ePnt(0) = center(0) + 60 : ePnt(1) = center(1) + 80 : ePnt(2) = 0</p><p> cirObj = blkObj.AddCircle(center, radius) '创建一个圆对象<br/> cirObj.color = ACAD_COLOR.acRed '将圆的颜色设为红色<br/> lineObj = blkObj.AddLine(sPnt, ePnt) '创建一条直线</p><p> blkRefObj = acaddoc.ModelSpace.InsertBlock(insertPnt, "TestBlock1", 1.0#, 1.0#, 1.0#, 0.0#) '插入图块<br/> Return blkRefObj<br/> End Function<br/>End Class</p> <p>十分感谢!!</p><p>学习一下</p> 我现在也用的是这种方法,但还是有一些缺点:<br/>1.需要删除添回循环; <br/>2.移动过的地方都会留下对象捕捉点,使CAD中的对象捕捉功能不能很好利用;<br/>3.不能像CAD中移动功能一样移动<br/><br/> 没有详细看!只是这类问题不能利用Jig类吗? 收藏下来了,有时间学习学习 <p>我赞成SIEBEN的想法,我利用EntityJig类已编出代码,也比较好用.网上有关于这方面的资料.</p> 这个只能说明是一个例子。实际应用是采用这样的方便是下策!
页:
[1]