Sometimes adding a point to a point group can be a bit of pain since you have to go into the Point Group Properties and add it in the Include Tab. This series of posts will go over how to add functionality where you can select a point, right click and have an option to add the point to a point group.
This post used a lot of code from Through The Interface which allows the code to run on a selected object. If an object is not selected then the user is prompted to select a point. The code starting on Line 151 creates a point group in the drawing. This is used in this code to create a point group if a point group does not exist and in a later post when the user selects the Point Group the point should go in. If you want to try it out before I finish the posts, you can download the compiled file here, type Netload to load the file and type AddPointToPointGroup at the command line to run the code. This code will only work in Civil 3D 2010. I still have to figure out how to modify the right click customization in the cui, so that part won’t work yet.
1:
2: Imports System.Runtime.InteropServices.Marshal
3: Imports Autodesk.AutoCAD.Runtime
4: Imports Autodesk.AutoCAD.ApplicationServices
5: Imports Autodesk.AutoCAD.DatabaseServices
6: Imports Autodesk.AutoCAD.EditorInput
7: Imports Autodesk.AutoCAD.Geometry
8: Imports Autodesk.Civil.DatabaseServices.Styles
9: Imports Autodesk.Civil.ApplicationServices
10: Imports Autodesk.Civil.Land.DatabaseServices
11: Imports AeccLandLib = Autodesk.AECC.Interop.Land
12: Imports AeccLandUi = Autodesk.AECC.Interop.UiLand
13: Imports AcadCom = Autodesk.AutoCAD.Interop
14: Imports System.Security
15:
16:
17: Public Class PointGroup
18: Implements Autodesk.AutoCAD.Runtime.IExtensionApplication
19:
20: Private m_doc As CivilDocument = Nothing
21: Private m_trans As Transaction = Nothing
22: Private m_Database As Database = Nothing
23: Private m_Editor As Editor = Nothing
24: Private Shared g_oCivil3DDoc As AeccLandUi.AeccDocument = Nothing
25:
26: Public Shared g_sName As String
27:
28: Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize
29:
30: End Sub
31:
32: Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate
33:
34: End Sub
35:
36: Public Sub GetCivilObject()
37: Dim docCol As Autodesk.AutoCAD.ApplicationServices.DocumentCollection = Application.DocumentManager
38: m_Database = docCol.MdiActiveDocument.Database
39: m_Editor = docCol.MdiActiveDocument.Editor
40: m_doc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument()
41: Dim oAcad As AcadCom.AcadApplication
42: Dim ACAD_VER As String = "AutoCAD.Application.18"
43: Dim CIVIL3D_VER As String = "AeccXUiLand.AeccApplication.7.0"
44: oAcad = GetActiveObject(ACAD_VER)
45: Dim oCivil3D As AeccLandUi.AeccApplication
46: oCivil3D = oAcad.GetInterfaceObject(CIVIL3D_VER)
47: If oCivil3D Is Nothing Then
48: MsgBox ("Error")
49: End If
50: g_oCivil3DDoc = oCivil3D.ActiveDocument
51: End Sub
52:
53: <CommandMethod("AddPointToPointGroup", CommandFlags.UsePickSet Or CommandFlags.Redraw Or CommandFlags.Modal)> _
54: Public Sub cmdAddPoint()
55:
56: Dim sName As String
57: Try
58: 'Get Civil 3D application, document and database
59: GetCivilObject()
60: m_trans = m_Database.TransactionManager.StartTransaction()
61: Dim oDoc As Document = Application.DocumentManager.MdiActiveDocument
62: Dim ed As Editor = oDoc.Editor
63: Try
64: Dim selectionRes As PromptSelectionResult = ed.SelectImplied()
65: If selectionRes.Status = PromptStatus.Error Then
66: ' Ask the user to select points.
67: Dim selectionOpts As New PromptSelectionOptions()
68: selectionOpts.MessageForAdding = vbLf & "Select point to add to Point Group: "
69: selectionRes = ed.GetSelection(selectionOpts)
70: Else
71: ' If there was a pickfirst set, clear it.
72: ed.SetImpliedSelection(New ObjectId(-1) {})
73: End If
74:
75: ' If the user hasn't cancelled.
76: If selectionRes.Status = PromptStatus.OK Then
77: ' Take the objects one by one.
78: Dim tr As Transaction = oDoc.TransactionManager.StartTransaction()
79: Try
80: Dim objIds As ObjectId() = selectionRes.Value.GetObjectIds()
81: For Each objId As ObjectId In objIds
82: Dim ent As Entity = DirectCast(tr.GetObject(objId, OpenMode.ForRead), Entity)
83: ' Check to see if the entity is a point.
84: If TypeOf ent Is PointEntity Then
85: Dim oPointGroups As AeccLandLib.AeccPointGroups
86: Dim oPoint As PointEntity
87: oPoint = ent
88: Dim frm As New FrmPointGroup
89: oPointGroups = g_oCivil3DDoc.PointGroups
90: Dim i As Integer
91: ' Fill out the form with the point group names.
92: For i = 0 To oPointGroups.Count - 1
93: If oPointGroups.Item(i).Name = "_All Points" Then
94: ' Don't do anything
95: Else
96: frm.ComboBox1.Items.Add(oPointGroups.Item(i).Name)
97: End If
98: Next
99: If oPointGroups.Count = 1 Then
100: sName = GetPointGroupName()
101: frm.ComboBox1.Items.Add(sName)
102: End If
103:
104: ' Set the combobox to the first item on the list.
105: frm.ComboBox1.SelectedIndex = 0
106: Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm)
107: ' Add the selected point to the indicated point group.
108: If g_sName = "Canceled Entered Into Value" Then
109: ' Don't do anything since the user cancled out of the dialog.
110: Else
111: Dim oPointGroup As AeccLandLib.AeccPointGroup
112: oPointGroup = oPointGroups.Item(g_sName)
113: Dim sIncludeNumbers As String
114: sIncludeNumbers = oPointGroup.QueryBuilder.IncludeNumbers
115: If sIncludeNumbers = "" Then
116: sIncludeNumbers = oPoint.PointNumber
117: Else
118: sIncludeNumbers = sIncludeNumbers & "," & oPoint.PointNumber
119: End If
120: oPointGroup.QueryBuilder.IncludeNumbers = sIncludeNumbers
121: End If
122:
123:
124: End If
125: Next
126: tr.Commit()
127: Catch ex As Autodesk.AutoCAD.Runtime.Exception
128: ed.WriteMessage(ex.Message)
129: tr.Abort()
130:
131: End Try
132: End If
133: Catch ex As Autodesk.AutoCAD.Runtime.Exception
134: ed.WriteMessage(ex.Message)
135: End Try
136:
137: m_trans.Commit()
138:
139: Catch ex As Exception
140: If m_trans IsNot Nothing Then m_trans.Abort()
141: Finally
142: If m_trans IsNot Nothing Then m_trans.Dispose()
143:
144: End Try
145:
146:
147: End Sub
148:
149:
150:
151: Public Shared Function GetPointGroupName() As String
152: Dim oDoc As Document = Application.DocumentManager.MdiActiveDocument
153: Dim ed As Editor = oDoc.Editor
154: Try
155: ' GetCivilObject()
156: Dim pr As PromptResult
157: pr = ed.GetString(vbCrLf & "Enter Point Group Name: ")
158: If pr.Status = PromptStatus.OK Then
159: Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
160: Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
161:
Using myt As Transaction = tm.StartTransaction()
162: Dim oPointGroups As AeccLandLib.AeccPointGroups
163: oPointGroups = g_oCivil3DDoc.PointGroups
164: oPointGroups.Add(pr.StringResult)
165: db.Dispose()
166: myt.Commit()
167: End Using
168: End If
169: Return pr.StringResult
170: Exit Function
171: GetPointGroupName = pr.StringResult
172: Catch ex As Autodesk.AutoCAD.Runtime.Exception
173: ed.WriteMessage(ex.Message)
174: GetPointGroupName = "DefaultPointGroupName"
175: End Try
176: GetPointGroupName = "DefaultPointGroupName"
177: End Function
178: End Class
http://blog.civil3dreminders.com/2009/05/add-point-to-point-group-part-ii.html
No comments:
Post a Comment