<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: reusing the portion of a sub of a commandmethod in another.. in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/reusing-the-portion-of-a-sub-of-a-commandmethod-in-another/m-p/5811942#M38709</link>
    <description>&lt;P&gt;Refactoring is another useful concept to study in this case.&lt;/P&gt;</description>
    <pubDate>Thu, 10 Sep 2015 22:09:29 GMT</pubDate>
    <dc:creator>dgorsman</dc:creator>
    <dc:date>2015-09-10T22:09:29Z</dc:date>
    <item>
      <title>reusing the portion of a sub of a commandmethod in another..</title>
      <link>https://forums.autodesk.com/t5/net-forum/reusing-the-portion-of-a-sub-of-a-commandmethod-in-another/m-p/5810397#M38707</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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

' &amp;lt;&amp;lt; one method here   , set the value of radius , stored it in hashtable to be used in other commands &amp;gt;&amp;gt;

&lt;STRONG&gt;&amp;lt;CommandMethod("sweep_a_cylinder")&amp;gt;
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 &amp;amp; "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 &amp;amp; "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 &amp;amp; "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&lt;/STRONG&gt;
Dim spline_and_polyline3d As DBObjectCollection = getting_arc_polyline(pt1, pt2, rad)

&lt;STRONG&gt;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

&lt;U&gt;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&lt;/U&gt;

btr.AppendEntity(ent)
tr.AddNewlyCreatedDBObject(ent, True)
tr.Commit()

End Using
End Sub&lt;/STRONG&gt;
'             %%%%%%%%% another command %%%%%%%%%%%%  


&lt;EM&gt;&amp;lt;CommandMethod("sweep_a_cylinder_cutoff")&amp;gt;
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 &amp;amp; "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 &amp;amp; "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 &amp;amp; "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&lt;/EM&gt;

Dim spline_and_polyline3d As DBObjectCollection = getting_arc_polyline_cutoff(pt1, pt2, rad)

&lt;EM&gt;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

&lt;U&gt;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&lt;/U&gt;

btr.AppendEntity(ent)
tr.AddNewlyCreatedDBObject(ent, True)
tr.Commit()

End Using
End Sub&lt;/EM&gt;


' getting_arc_polylinefunction is here

' getting_arc_polyline_cutoff

end class
end namespace&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The above is my code written in VB.NET and I have a few questions.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;(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&amp;nbsp;&lt;/SPAN&gt;in the sub. &amp;nbsp;Is there a way to reduce the redundancy because I feel it is rather pointless to write the same code twice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(2). I have to set different colours for the resulting solid3d &amp;nbsp;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any kind of help and suggestion would be very much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2015 05:50:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reusing-the-portion-of-a-sub-of-a-commandmethod-in-another/m-p/5810397#M38707</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-09-10T05:50:29Z</dc:date>
    </item>
    <item>
      <title>Re: reusing the portion of a sub of a commandmethod in another..</title>
      <link>https://forums.autodesk.com/t5/net-forum/reusing-the-portion-of-a-sub-of-a-commandmethod-in-another/m-p/5810429#M38708</link>
      <description>You are right, it's not only pointless it's almost for bitten to duplicate code &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;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.&lt;BR /&gt;</description>
      <pubDate>Thu, 10 Sep 2015 06:23:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reusing-the-portion-of-a-sub-of-a-commandmethod-in-another/m-p/5810429#M38708</guid>
      <dc:creator>SENL1362</dc:creator>
      <dc:date>2015-09-10T06:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: reusing the portion of a sub of a commandmethod in another..</title>
      <link>https://forums.autodesk.com/t5/net-forum/reusing-the-portion-of-a-sub-of-a-commandmethod-in-another/m-p/5811942#M38709</link>
      <description>&lt;P&gt;Refactoring is another useful concept to study in this case.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2015 22:09:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/reusing-the-portion-of-a-sub-of-a-commandmethod-in-another/m-p/5811942#M38709</guid>
      <dc:creator>dgorsman</dc:creator>
      <dc:date>2015-09-10T22:09:29Z</dc:date>
    </item>
  </channel>
</rss>

