AutoCAD 二次开发封装动态JIG

更新时间:2023-11-17 06:59:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

使用VB.Net开发AutoCAD 2010 ,动态拖动类的封装代码 Imports System

Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD.GraphicsInterface Imports Autodesk.AutoCAD.Runtime Imports System.Drawing Imports System.Windows.Forms '''

''' 封装简化Jig操作,函数式实现DrawJig '''

''' Public Class Drag Inherits DrawJig

Private Delegate Function AcquireMod(prompts As JigPrompts) As SamplerStatus

Private Shared _rstAsPromptResult

Private Shared _point3d As Point3d

Private Shared _PointOptionsAsJigPromptPointOptions

Private Shared _double As Double

Private Shared _DistanceOptionsAsJigPromptDistanceOptions

Private Shared _AngleOptionsAsJigPromptAngleOptions

Private Shared _cur As Point

Private Shared _StringOptionsAsJigPromptStringOptions

Private Shared _callBack As Action(Of Drag.Result)

Private Shared _acquireModAsDrag.AcquireMod

Protected Sub New()

End Sub

Protected Overrides Function WorldDraw(draw As WorldDraw) As Boolean

Drag._callBack(New Drag.Result(Drag._rst, draw))

Return True

End Function

Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus

Return Drag._acquireMod(prompts)

End Function

#Region \定义各种Jig模式\

Private Shared Function GetPoint(prompts As JigPrompts) As SamplerStatus

Dim rst As PromptPointResult = prompts.AcquirePoint(Drag._PointOptions)

Drag._rst = rst

If rst.Value<> Drag._point3d Then

Drag._point3d = rst.Value

Return SamplerStatus.OK

Else

Return SamplerStatus.NoChange

End If

End Function

Private Shared Function GetDistance(prompts As JigPrompts) As SamplerStatus

Dim

rst

As

PromptDoubleResult

prompts.AcquireDistance(Drag._DistanceOptions)

Drag._rst = rst

If rst.Value<>Drag._double Then

Drag._double = rst.Value

Return SamplerStatus.OK

Else

=

Return SamplerStatus.NoChange

End If

End Function

Private Shared Function GetAngle(prompts As JigPrompts) As SamplerStatus

Dim rst As PromptDoubleResult = prompts.AcquireAngle(Drag._AngleOptions)

Drag._rst = rst

If rst.Value<>Drag._double Then

Drag._double = rst.Value

Return SamplerStatus.OK

Else

Return SamplerStatus.NoChange

End If

End Function

Private Shared Function GetString(prompts As JigPrompts) As SamplerStatus

Dim rst As PromptResult = prompts.AcquireString(Drag._StringOptions)

Drag._rst = rst

Dim cur As Point = Control.MousePosition

If Control.MousePosition<>Drag._cur Then

Drag._cur = cur

Return SamplerStatus.OK

Else

Return SamplerStatus.NoChange

End If

End Function

#End Region

'''

''' 执行Jig拖动 '''

''' ''' ''' '''

Public Shared Function StartDrag(options As JigPromptOptions, Action(Of Drag.Result)) As PromptResult

If TypeOf options Is JigPromptPointOptions Then

_PointOptions = (TryCast(options, JigPromptPointOptions))

_acquireMod = New AcquireMod(AddressOfGetPoint)

ElseIfTypeOf options Is JigPromptDistanceOptions Then

callFun As

_DistanceOptions = (TryCast(options, JigPromptDistanceOptions))

_acquireMod = New Drag.AcquireMod(AddressOfGetDistance)

ElseIfTypeOf options Is JigPromptAngleOptions Then

_AngleOptions = (TryCast(options, JigPromptAngleOptions))

_acquireMod = New AcquireMod(AddressOfGetAngle)

ElseIfTypeOf options Is JigPromptStringOptions Then

_StringOptions = (TryCast(options, JigPromptStringOptions))

_acquireMod = New AcquireMod(AddressOfGetString)

End If

_callBack = callFun

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocum

ent.Editor.Drag(New Drag())

_callBack(New Result(_rst, Nothing))

Return _rst

End Function

'''

''' 执行Jig拖动[Point模式] '''

''' ''' ''' ''' '''

Public Shared Function StartDrag(msg As String, kwd As String, callFun As Action(Of Drag.Result)) As PromptResult

Return StartDrag(New JigPromptPointOptions(msg, kwd), callFun)

End Function

'''

''' 执行Jig拖动[Point模式] '''

''' ''' ''' '''

Public Shared Function StartDrag(msg As String, callFun As Action(Of Drag.Result)) As PromptResult

Return Drag.StartDrag(New JigPromptPointOptions(msg), callFun)

End Function

'''

''' 回调函数的参数 '''

''' Public Class Result

Private rstAsPromptResult

Private draw AsWorldDraw

Public ReadOnly Property PromptResult() As PromptResult

Get

Return rst

End Get

End Property

Public ReadOnly Property Geometry() As Geometry

Get

Return Me.draw.Geometry

End Get

End Property

Public ReadOnly Property Point() As Point3d

Get

Return Drag._point3d

End Get

End Property

Public ReadOnly Property Dist() As Double

Get

Return Drag._double

End Get

End Property

Public ReadOnly Property Angle() As Double

Get

Return Drag._double

End Get

End Property

Public ReadOnly Property Status() As PromptStatus

Get

Return rst.Status

End Get

End Property

Public ReadOnly Property [String]() As String

Get

Return rst.StringResult

End Get

End Property

Friend Sub New(promptResult As PromptResult, worldDraw As WorldDraw)

rst = promptResult

draw = worldDraw

End Sub

Public Sub DrawEnt(entity As Drawable)

If draw IsNotNothing Then

draw.Geometry.Draw(entity)

End If

End Sub

End Class

End Class

测试代码:

Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD.GraphicsInterface Imports Autodesk.AutoCAD.Runtime Imports YJSCAD Public Class Class1

Dim cir As New Circle(New Point3d(), Vector3d.ZAxis, 10.0)

If Drag.StartDrag(vbCrLf& \指定圆中心点\cir.Center = rst.Point rst.DrawEnt(cir)

End Sub).Status = PromptStatus.OK Then

Dim

result

As

PromptResult

=

Drag.StartDrag(New

JigPromptDistanceOptions(vbCrLf& \指定圆半径\

Sub(rst As Drag.Result)

cir.Radius = If((rst.Dist = 0), 0.000001, rst.Dist)

rst.DrawEnt(cir)

If result.Status = PromptStatus.OK Then

ShareTool.AddToModelSpace(cir)

End If

End If

End Sub

End Class

End Sub)

Sub(rst As Drag.Result)

cir.Radius = If((rst.Dist = 0), 0.000001, rst.Dist)

rst.DrawEnt(cir)

If result.Status = PromptStatus.OK Then

ShareTool.AddToModelSpace(cir)

End If

End If

End Sub

End Class

End Sub)

本文来源:https://www.bwwdw.com/article/mrfv.html

Top