reusing the portion of a sub of a commandmethod in another..

reusing the portion of a sub of a commandmethod in another..

Anonymous
Not applicable
542 Views
2 Replies
Message 1 of 3

reusing the portion of a sub of a commandmethod in another..

Anonymous
Not applicable

 

 

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry


Namespace ACADProtection
Public Class AutoCAD_RS_Commands
Public Const Key_For_Protection_Class As String = "The protection class used"
Public Const pi = Math.PI

' << one method here   , set the value of radius , stored it in hashtable to be used in other commands >>

<CommandMethod("sweep_a_cylinder")>
Public Sub SweepAlongCylinder()

Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim ud As Hashtable = doc.UserData
Dim rad As Double
rad = ud(Key_For_Protection_Class)

Using tr As Transaction = db.TransactionManager.StartTransaction()

Dim ppo As PromptPointOptions = New PromptPointOptions(vbLf & "choose/click the centre of the base circle:")
Dim ppr As PromptPointResult = doc.Editor.GetPoint(ppo)
Dim pt As Point3d = ppr.Value
If ppr.Status = PromptStatus.Cancel Then Exit Sub

Dim pdo1 As PromptDoubleOptions = New PromptDoubleOptions(vbLf & "The radius of the cylinder? ")
Dim pdr1 As PromptDoubleResult = doc.Editor.GetDouble(pdo1)
Dim pd1 As Double = pdr1.Value
If pdr1.Status = PromptStatus.Cancel Then Exit Sub

Dim pdo2 As PromptDoubleOptions = New PromptDoubleOptions(vbLf & "The height of the cylinder? ")
Dim pdr2 As PromptDoubleResult = doc.Editor.GetDouble(pdo2)
Dim pd2 As Double = pdr2.Value
If pdr2.Status = PromptStatus.Cancel Then Exit Sub

'setting the points to be passed to functions
Dim pt1 As Point3d = New Point3d(pt.X + pd1, pt.Y, pt.Z) 'look in the picture
Dim pt2 As Point3d = New Point3d(pt.X + pd1, pt.Y, pt.Z + pd2)
Dim pt4 As Point3d = New Point3d(pt.X, pt.Y, pt.Z + pd2)

'construct another circle on top
Dim Cir2 As Circle = New Circle()
Cir2.Center = pt4
Cir2.Normal = New Vector3d(0, 0, 1)
Cir2.Radius = pd1
Dim spline_and_polyline3d As DBObjectCollection = getting_arc_polyline(pt1, pt2, rad)

Dim spl1 As Arc = arc_and_polyline(0)
Dim pl3d1 As Polyline = arc_and_polyline(1)

Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

btr.AppendEntity(spl1)
tr.AddNewlyCreatedDBObject(spl1, False)            'can be removed??
btr.AppendEntity(pl3d1)
tr.AddNewlyCreatedDBObject(pl3d1, False)         'can be removed??
pl3d1.JoinEntity(spl1)

Dim curUCSMatrix As Matrix3d = ed.CurrentUserCoordinateSystem
Dim curUCS As CoordinateSystem3d = curUCSMatrix.CoordinateSystem3d
pl3d1.TransformBy(Matrix3d.Rotation(pi / 2, curUCS.Xaxis, pt1))

Dim ent As Entity
Dim sob As SweepOptionsBuilder = New SweepOptionsBuilder
Dim sol As Solid3d = New Solid3d
sol.CreateSweptSolid(pl3d1, Cir2, sob.ToSweepOptions)
ent = sol

If rad = 20000 Then
ent.ColorIndex = 1
ElseIf rad = 30000 Then
ent.ColorIndex = 2
ElseIf rad = 45000 Then
ent.ColorIndex = 3
ElseIf rad = 60000 Then
ent.ColorIndex = 4
End If

btr.AppendEntity(ent)
tr.AddNewlyCreatedDBObject(ent, True)
tr.Commit()

End Using
End Sub
'             %%%%%%%%% another command %%%%%%%%%%%%  


<CommandMethod("sweep_a_cylinder_cutoff")>
Public Sub SweepAlongCylinderWithCutoff()

Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim ud As Hashtable = doc.UserData
Dim rad As Double
rad = ud(Key_For_Protection_Class)
 
Using tr As Transaction = db.TransactionManager.StartTransaction()
 
Dim ppo As PromptPointOptions = New PromptPointOptions(vbLf & "choose/click the centre of the base circle:")
Dim ppr As PromptPointResult = doc.Editor.GetPoint(ppo)
Dim pt As Point3d = ppr.Value
If ppr.Status = PromptStatus.Cancel Then Exit Sub
 
Dim pdo1 As PromptDoubleOptions = New PromptDoubleOptions(vbLf & "The radius of the cylinder? ")
Dim pdr1 As PromptDoubleResult = doc.Editor.GetDouble(pdo1)
Dim pd1 As Double = pdr1.Value
If pdr1.Status = PromptStatus.Cancel Then Exit Sub
 
Dim pdo2 As PromptDoubleOptions = New PromptDoubleOptions(vbLf & "The height of the cylinder? ")
Dim pdr2 As PromptDoubleResult = doc.Editor.GetDouble(pdo2)
Dim pd2 As Double = pdr2.Value
If pdr2.Status = PromptStatus.Cancel Then Exit Sub
 
'setting the points to be passed to functions
Dim pt1 As Point3d = New Point3d(pt.X + pd1, pt.Y, pt.Z) 'look in the picture
Dim pt2 As Point3d = New Point3d(pt.X + pd1, pt.Y, pt.Z + pd2)
Dim pt4 As Point3d = New Point3d(pt.X, pt.Y, pt.Z + pd2)
 
'construct another circle on top
Dim Cir2 As Circle = New Circle()
Cir2.Center = pt4
Cir2.Normal = New Vector3d(0, 0, 1)
Cir2.Radius = pd1

Dim spline_and_polyline3d As DBObjectCollection = getting_arc_polyline_cutoff(pt1, pt2, rad)

Dim spl1 As Arc = arc_and_polyline(0)
Dim pl3d1 As Polyline = arc_and_polyline(1)
 
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
 
btr.AppendEntity(spl1)
tr.AddNewlyCreatedDBObject(spl1, False)            'can be removed??
btr.AppendEntity(pl3d1)
tr.AddNewlyCreatedDBObject(pl3d1, False)         'can be removed??
pl3d1.JoinEntity(spl1)
 
Dim curUCSMatrix As Matrix3d = ed.CurrentUserCoordinateSystem
Dim curUCS As CoordinateSystem3d = curUCSMatrix.CoordinateSystem3d
pl3d1.TransformBy(Matrix3d.Rotation(pi / 2, curUCS.Xaxis, pt1))
 
Dim ent As Entity
Dim sob As SweepOptionsBuilder = New SweepOptionsBuilder
Dim sol As Solid3d = New Solid3d
sol.CreateSweptSolid(pl3d1, Cir2, sob.ToSweepOptions)
ent = sol

If rad = 20000 Then
ent.ColorIndex = 1
ElseIf rad = 30000 Then
ent.ColorIndex = 2
ElseIf rad = 45000 Then
ent.ColorIndex = 3
ElseIf rad = 60000 Then
ent.ColorIndex = 4
End If

btr.AppendEntity(ent)
tr.AddNewlyCreatedDBObject(ent, True)
tr.Commit()

End Using
End Sub


' getting_arc_polylinefunction is here

' getting_arc_polyline_cutoff

end class
end namespace

 

The above is my code written in VB.NET and I have a few questions.

 

(1). There are two very similar commands (one highlighted in Bold and another in italic). The only difference between them is that two different functions are called in the sub.  Is there a way to reduce the redundancy because I feel it is rather pointless to write the same code twice.

 

(2). I have to set different colours for the resulting solid3d  depending on the size of radius chosen. Can I also make it more efficient for I don't think writing it in all customized commands(there are as many as 20) is not a good programming practice?

 

Any kind of help and suggestion would be very much appreciated.

 

 

 

 

 

 

0 Likes
543 Views
2 Replies
Replies (2)
Message 2 of 3

SENL1362
Advisor
Advisor
You are right, it's not only pointless it's almost for bitten to duplicate code 🙂
However to my opinion this not a AutoCAD specific subject but more general programming specific. So may i recommend you to read general .NET books/articles, particular regarding Classes.
0 Likes
Message 3 of 3

dgorsman
Consultant
Consultant

Refactoring is another useful concept to study in this case.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes