抛砖引玉
Module mTlsCad
Public Const strAppName As String = "TlsCad"
Public Const tString As Short = 0
Public Const tDouble As Short = 1
Public Const tPoint As Short = 2
Public Function LeftStr(ByVal String1 As Object, ByVal String2 As Object) As Object
Try
LeftStr = Left(String1, InStr(String1, String2) - 1)
Catch ex As Exception
LeftStr = False
End Try
End Function
Public Function RightStr(ByVal String1 As Object, ByVal String2 As Object) As Object
Try
RightStr = Right(String1, Len(String1) - Len(String2) - InStr(String1, String2) + 1)
Catch ex As Exception
RightStr = False
End Try
End Function
End Module Public Class TlsApplication
<CommandMethod("TlsCadRun")> Public Shared Sub TlsCadRun()
Dim pEntity As New TlsEntity
Dim aSub As String
Dim pACadApp As AcadApplication = Application.AcadApplication
Dim pACadDoc As AcadDocument = pACadApp.ActiveDocument
aSub = pACadDoc.Utility.GetString(0, "请输入函数:")
pEntity.Init()
pEntity.RunSub(aSub)
pEntity.Insert()
End Sub End Class Public Class TlsData
Public Type As Short
Private p_sData As String
Private p_dData As Double
Private p_pData As Object
Public Property sData() As String
Get
sData = p_sData
End Get
Set(ByVal Value As String)
Dim pnt As Object
Dim i As Object
Dim pStr As String
p_sData = Value
pStr = Left(Value, 1)
Type = tString
If pStr >= "0" And pStr <= "9" Then
If InStr(1, Value, ",") > 0 Then
pnt = Split(Value, ",")
For i = 0 To 2
p_pData(i) = pnt(i)
Next i
Type = tPoint
Else
p_dData = Val(Value)
Type = tDouble
End If
End If
End Set
End Property
Public Property X() As Double
Get
X = p_pData(0)
End Get
Set(ByVal Value As Double)
p_pData(0) = Value
End Set
End Property
Public Property Y() As Double
Get
Y = p_pData(1)
End Get
Set(ByVal Value As Double)
p_pData(1) = Value
End Set
End Property
Public Property Z() As Double
Get
Z = p_pData(2)
End Get
Set(ByVal Value As Double)
p_pData(2) = Value
End Set
End Property
Public Property dData() As Double
Get
dData = p_dData
End Get
Set(ByVal Value As Double)
p_dData = Value
End Set
End Property
Public Property pData() As Object
Get
pData = p_pData
End Get
Set(ByVal Value As Object)
p_pData = Value
End Set
End Property
Public Sub New()
Dim pnt(2) As Double
p_pData = pnt
End Sub
End Class Public Class TlsObject
Protected Name As String = "TlsObject"
Protected pACadApp As AcadApplication = Application.AcadApplication
Protected pACadDoc As AcadDocument = pACadApp.ActiveDocument
Protected pACadMSpace As Autodesk.AutoCAD.Interop.Common.AcadModelSpace = pACadApp.ActiveDocument.ModelSpace
Protected pObj As Autodesk.AutoCAD.Interop.Common.AcadObject
Protected Overridable Sub Save()
Dim hDataType(1) As Short, hData(1) As Object
hDataType(0) = 1001 : hData(0) = strAppName
hDataType(1) = 1000 : hData(1) = Name
pObj.SetXData(hDataType, hData)
End Sub
Public Overridable Sub Insert()
End Sub
Public Overridable Sub Change()
End Sub
End Class Public Class TlsEntity<BR> Inherits TlsObject<BR> Private pTlsVals As New Collection<BR> '变量集合<BR> Private Shared pSubs As New Collection<BR> '函数集合<BR> Private Shared pBlock As Autodesk.AutoCAD.Interop.Common.AcadBlock<BR> Private pMirrorStart, pArrayStart As Short<BR> Private dDataType(50) As Short, dData(50) As Object<BR> Public Sub New()<BR> Dim i As Object<BR> Dim pData As TlsData<BR> Name = "TlsEntity"<BR> pData = New TlsData : pData.sData = "0,0,0" : pTlsVals.Add(pData, "PS")<BR> '定义初始点<BR> pData = New TlsData : pData.sData = "0" : pTlsVals.Add(pData, "Start")<BR> '旋转或镜像开始<BR> pData = New TlsData : pData.sData = "0" : pTlsVals.Add(pData, "End")<BR> '旋转或镜像结束<BR> pData = New TlsData : pData.sData = "01" : pTlsVals.Add(pData, "CSX")<BR> pData = New TlsData : pData.sData = "02" : pTlsVals.Add(pData, "XSX")<BR> pData = New TlsData : pData.sData = "03" : pTlsVals.Add(pData, "ZXX")<BR> pData = New TlsData : pData.sData = "04" : pTlsVals.Add(pData, "XX")<BR> '图层定义<BR> dDataType(0) = 1001 : dData(0) = "EntityDefine"<BR> dDataType(1) = 1000 : dData(1) = ""<BR> For i = 2 To 30<BR> dDataType(i) = 1040 : dData(i) = 0<BR> Next i<BR> For i = 31 To 50<BR> dDataType(i) = 1070 : dData(i) = 0<BR> Next i<BR> '扩张数据初始化
End Sub<BR> Public Sub Init()<BR> '块初始化<BR> Dim pnt(2) As Double<BR> OpenFile()<BR> pBlock = pACadDoc.Blocks.Add(pnt, "*U")<BR> End Sub<BR> Public Property EntityName() As String<BR> '实体名属性<BR> Get<BR> EntityName = dData(1)<BR> End Get<BR> Set(ByVal Value As String)<BR> dData(1) = Value<BR> End Set<BR> End Property<BR> Private Property XData(ByVal Index As Short) As Double<BR> '实体参数属性<BR> Get<BR> XData = dData(Index + 10)<BR> End Get<BR> Set(ByVal Value As Double)<BR> dData(Index + 10) = Value<BR> End Set<BR> End Property<BR> Protected Overrides Sub Save()<BR> MyBase.Save()<BR> pObj.SetXData(dDataType, dData)<BR> End Sub<BR> Public Overrides Sub Insert()<BR> Dim pnt As Object<BR> Try<BR> pnt = pACadDoc.Utility.GetPoint(, "请输入插入点:")<BR> pObj = pACadMSpace.InsertBlock(pnt, pBlock.Name, 1, 1, 1, 0)<BR> pObj.Rotate(pnt, pACadDoc.Utility.GetAngle(pnt, "请输入旋转角度:"))<BR> Save()<BR> Catch ex As Exception<BR> End Try<BR> End Sub<BR> Private Sub StrCal(ByVal String1 As Object)<BR> '函数运算器<BR> Dim i As Object<BR> Dim pCals As Collection<BR> Dim pStrack1 As New Collection<BR> Dim pStrack2 As Collection<BR> Try<BR> pCals = CutStr(String1)<BR> '分解一行语句<BR> For i = 1 To pCals.Count<BR> If pCals(i).sData <> ")" Then<BR> '不是)时顺序入栈<BR> pStrack1.Add(pCals(i))<BR> Else<BR> '遇")"时顺序出栈到"("<BR> pStrack2 = New Collection<BR> Do While pStrack1(pStrack1.Count).sData <> "("<BR> pStrack2.Add(pStrack1(pStrack1.Count))<BR> pStrack1.Remove(pStrack1.Count)<BR> Loop<BR> pStrack1.Remove(pStrack1.Count)<BR> pStrack1.Add(GetVal(pStrack2))<BR> '计算结果并入栈<BR> End If<BR> Next i<BR> Catch ex As Exception<BR> End Try<BR> End Sub<BR> Private Function CutStr(ByVal String1 As Object) As Collection<BR> '分解一行语句<BR> Dim i, j As Object<BR> Dim pStrack As Object<BR> Dim pCals As New Collection<BR> Dim pData As TlsData<BR> Try<BR> pStrack = Split(String1, " ")<BR> '先按空格分解<BR> If Not IsArray(pStrack) Then Exit Try<BR> For Each i In pStrack<BR> If InStr(1, i, "(") = 1 Then<BR> '有"("时<BR> pData = New TlsData<BR> pData.sData = "("<BR> pCals.Add(pData)<BR> If RightStr(i, "(") <> "" Then<BR> pData = New TlsData<BR> pData.sData = RightStr(i, "(")<BR> pCals.Add(pData)<BR> End If<BR> ElseIf InStr(1, i, ")") > 1 Then<BR> '有")"时<BR> pData = New TlsData<BR> pData.sData = LeftStr(i, ")")<BR> pCals.Add(pData)<BR> For j = 1 To Len(RightStr(i, ")")) + 1<BR> pData = New TlsData<BR> pData.sData = ")"<BR> pCals.Add(pData)<BR> Next j<BR> ElseIf Trim(i) <> "" Then<BR> '去除空字符<BR> pData = New TlsData<BR> pData.sData = i<BR> pCals.Add(pData)<BR> End If<BR> Next i<BR> Catch ex As Exception<BR> Finally<BR> CutStr = pCals<BR> End Try<BR> End Function<BR> Private Function GetVal(ByVal Strack As Collection) As TlsData<BR> '计算函数结果<BR> Dim pData As New TlsData<BR> Dim pCals As New Collection<BR> Dim Count As Integer<BR> Dim pMinCount As Integer<BR> Dim i As Object<BR> Try<BR> Count = Strack.Count<BR> For i = Count To 1 Step -1<BR> pCals.Add(Strack(i))<BR> Next i<BR> pMinCount = 2 : If pCals(1).sData.ToUpper = "SUB" Or pCals(1).sData.ToUpper = "CALL" Then pMinCount = 3<BR> If UCase(pCals(1).sData) = "VALUE" Or UCase(pCals(1).sData) = "SUB" Then<BR> For i = pMinCount To Count<BR> If InStr(pCals(i).sData, "#") > 0 Then<BR> pData = New TlsData : pData.sData = "0,0,0"<BR> pTlsVals.Add(pData, LeftStr(pCals(i).sData, "#"))<BR> Else<BR> pData = New TlsData : pData.sData = "0"<BR> pTlsVals.Add(pData, pCals(i).sData)<BR> End If<BR> Next i<BR> Exit Function<BR> End If<BR> For i = pMinCount To Count<BR> If pCals(i).Type = tString Then<BR> pCals(i).dData = pTlsVals(pCals(i).sData).dData<BR> pCals(i).pData = pTlsVals(pCals(i).sData).pData<BR> pCals(i).Type = pTlsVals(pCals(i).sData).Type<BR> End If<BR> Next i<BR> Select Case pCals(1).sData.ToUpper<BR> Case "="<BR> If Count = 6 Then<BR> pTlsVals(pCals(2).sData).X = pCals(3).X + pCals(4).dData<BR> pTlsVals(pCals(2).sData).Y = pCals(3).Y + pCals(5).dData<BR> pTlsVals(pCals(2).sData).Z = pCals(3).Z + pCals(6).dData<BR> Else<BR> pTlsVals(pCals(2).sData).dData = pCals(3).dData<BR> pTlsVals(pCals(2).sData).pData = pCals(3).pData<BR> End If<BR> pData = pTlsVals(pCals(2).sData)<BR> Case "+"<BR> pData.sData = "0"<BR> pData.dData = pCals(2).dData + pCals(3).dData<BR> Case "-"<BR> pData.sData = "0"<BR> If Count = 3 Then<BR> pData.dData = pCals(2).dData - pCals(3).dData<BR> Else<BR> pData.dData = -pCals(2).dData<BR> End If<BR> Case "*"<BR> pData.sData = "0"<BR> pData.dData = pCals(2).dData * pCals(3).dData<BR> Case "/"<BR> pData.sData = "0"<BR> pData.dData = pCals(2).dData / pCals(3).dData<BR> Case "^"<BR> pData.sData = "0"<BR> pData.dData = pCals(2).dData ^ pCals(3).dData<BR> Case "CALL"<BR> Dim pTlsEntity As New TlsEntity<BR> Dim pValues As String<BR> pValues = pCals(2).sData + "("<BR> For i = 3 To pCals.Count - 1<BR> pValues = pValues + Convert.ToString(pCals(i).dData) + ","<BR> Next i<BR> pValues = pValues + Convert.ToString(pCals(pCals.Count).dData) + ")"<BR> pTlsEntity.RunSub(pValues)<BR> Case "LINE"<BR> pBlock.AddLine(pCals(2).pData, pCals(3).pData).Layer = pTlsVals(pCals(4).sData).sData<BR> Case "CIRCLE"<BR> pBlock.AddCircle(pCals(2).pData, pCals(3).dData).Layer = pTlsVals(pCals(4).sData).sData<BR> Case "ELLIPSE"<BR> Dim obj As Autodesk.AutoCAD.Interop.Common.AcadEllipse<BR> obj = pBlock.AddEllipse(pCals(2).pData, pCals(3).pData, pCals(4).dData)<BR> obj.StartAngle = pCals(5).dData / 45 * System.Math.Atan(1)<BR> obj.EndAngle = pCals(6).dData / 45 * System.Math.Atan(1)<BR> obj.Layer = pTlsVals(pCals(7).sData).sData<BR> Case "ARC"<BR> pBlock.AddArc(pCals(2).pData, pCals(3).dData, pCals(4).dData / 45 * System.Math.Atan(1), pCals(5).dData / 45 * System.Math.Atan(1)).Layer = pTlsVals(pCals(6).sData).sData<BR> Case "MIRROR"<BR> If pCals(2).sData = "Start" Then<BR> pMirrorStart = pBlock.Count<BR> Else<BR> For i = pMirrorStart To pBlock.Count - 1<BR> pBlock.Item(i).Mirror(pCals(2).pData, pCals(3).pData)<BR> Next i<BR> End If<BR> Case "ARRAY"<BR> If pCals(2).sData = "Start" Then<BR> pArrayStart = pBlock.Count<BR> Else<BR> If Count = 4 Then<BR> For i = pArrayStart To pBlock.Count - 1<BR> pBlock.Item(i).ArrayPolar(pCals(3).dData, pCals(4).dData, pCals(2).pData)<BR> Next i<BR> Else<BR> For i = pArrayStart To pBlock.Count - 1<BR> pBlock.Item(i).ArrayRectangular(pCals(2).dData, pCals(3).dData, 1, pCals(4).dData, pCals(5).dData, 0)<BR> Next i<BR> End If<BR> End If<BR> Case "HATCH"<BR> End Select<BR> Catch ex As Exception<BR> Finally<BR> GetVal = pData<BR> End Try<BR> End Function<BR> Private Sub OpenFile()<BR> '读取函数文件<BR> Dim i As Object<BR> Dim ts As StreamReader<BR> Dim Data As String<BR> Dim allSub As Object<BR> Dim aSub As Collection<BR> Dim FileName As String<BR> For i = 1 To pSubs.Count<BR> pSubs.Remove(1)<BR> Next i<BR> Try<BR> ts = File.OpenText(Directory.GetCurrentDirectory & "\TlsCad.Sub")<BR> allSub = Split(ts.ReadToEnd, vbCrLf)<BR> For i = 0 To UBound(allSub)<BR> Data = allSub(i)<BR> If InStr(Data, "(Sub") = 1 Then<BR> '向函数集合加入新函数<BR> aSub = New Collection<BR> pSubs.Add(aSub, CutStr(Data)(3).sData)<BR> pSubs(pSubs.Count).Add(Data)<BR> Else<BR> pSubs(pSubs.Count).Add(Data)<BR> End If<BR> Next i<BR> Catch ex As Exception<BR> Finally<BR> ts.Close()<BR> End Try<BR> End Sub<BR> Public Sub RunSub(ByVal String1 As String)<BR> Dim i As Object<BR> Dim aSub As Collection<BR> Dim pValues As Object<BR> Try<BR> EntityName = Trim(LeftStr(String1, "(")).ToUpper<BR> aSub = pSubs(EntityName)<BR> StrCal(aSub(1))<BR> '分配函数变量<BR> pValues = Split(LeftStr(RightStr(String1, "("), ")"), ",")<BR> If IsArray(pValues) Then<BR> '初始化函数变量<BR> For i = 0 To UBound(pValues)<BR> pTlsVals(i + 8).dData = pValues(i)<BR> XData(i) = pValues(i)<BR> Next i<BR> End If<BR> For i = 2 To aSub.Count<BR> '依次运行语句<BR> StrCal(aSub(i))<BR> Next i<BR> Catch ex As Exception<BR> End Try<BR> End Sub
End Class<BR> 这是我编的通用参数驱动程序,用的还是ActiveX方法
将下面的文本存为TlsCad.sub文件和编译后的Dll文件放在同一目录下就可以使用了
(Sub TYFT_EHA Dn H h1 d)<BR>(Value a1 a2 a3 a4 a5 a6 p1# p2#)<BR>(= a3 (- (= a2 (/ (= a1 (+ Dn (* d 2))) 2))))<BR>(= a4 (/ (* (- H h1 ) 2) Dn))<BR>(= a5 (/ (* (+ (- H h1) d) 2) a1))<BR>(= a6 (/ Dn 2))<BR>(= p2 (= p1 ps a3 0 0) 0 h1 0)<BR>(Line p1 p2 CSX)<BR>(= p2 (= p1 p1 d 0 0) 0 h1 0)<BR>(Line p1 p2 CSX)<BR>(= p2 (= p1 p1 Dn 0 0) 0 h1 0)<BR>(Line p1 p2 CSX)<BR>(= p2 (= p1 p1 d 0 0) 0 h1 0)<BR>(Line p1 p2 CSX)<BR>(= p2 (= p1 ps a3 0 0) a1 0 0)<BR>(Line p1 p2 CSX)<BR>(= p1 ps 0 h1 0)<BR>(= p2 ps a6 0 0)<BR>(Ellipse p1 p2 a4 0 180 CSX)<BR>(= p2 ps a2 0 0)<BR>(Ellipse p1 p2 a5 0 180 CSX)<BR>(End Sub)<BR>(Sub TYFT_EHB Dn H h1 d)<BR>(Value a1 a2 a3 a4 a5 a6 p1# p2#)<BR>(= Dn (- Dn (* 2 d)))<BR>(= H (- H d))<BR>(= a3 (- (= a2 (/ (= a1 (+ Dn (* d 2))) 2))))<BR>(= a4 (/ (* (- H h1 ) 2) Dn))<BR>(= a5 (/ (* (+ (- H h1) d) 2) a1))<BR>(= a6 (/ Dn 2))<BR>(= p2 (= p1 ps a3 0 0) 0 h1 0)<BR>(Line p1 p2 CSX)<BR>(= p2 (= p1 p1 d 0 0) 0 h1 0)<BR>(Line p1 p2 CSX)<BR>(= p2 (= p1 p1 Dn 0 0) 0 h1 0)<BR>(Line p1 p2 CSX)<BR>(= p2 (= p1 p1 d 0 0) 0 h1 0)<BR>(Line p1 p2 CSX)<BR>(= p2 (= p1 ps a3 0 0) a1 0 0)<BR>(Line p1 p2 CSX)<BR>(= p1 ps 0 h1 0)<BR>(= p2 ps a6 0 0)<BR>(Ellipse p1 p2 a4 0 180 CSX)<BR>(= p2 ps a2 0 0)<BR>(Ellipse p1 p2 a5 0 180 CSX)<BR>(End Sub)
命令格式:
TlsCadRun
tyft_eha(300,100,25,4) 好贴! 不明白怎么用? 把代码全部复制到TlsCad.vb文件内,加入下列代码
Imports Autodesk.AutoCAD.ApplicationServices<BR>Imports Autodesk.AutoCAD.Runtime<BR>Imports Autodesk.AutoCAD.Interop<BR>Imports System.IO<BR>编译为Dll文件,用NetLoad加载一下 我水平低,还不能看懂,有一个问题先问lzh741206一下:Public Const strAppName As String = "TlsCad"在VB。NET中是不是可在声明的同时赋值?!
页:
[1]
2