Using AcadSelectionSet to export selected 3dSolid to ACIS SAT file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have a task that requires me to automate the export of a DWG to SAT without user interaction. I have to find a visible 3DSolid in the DWG database and select it programatically as users do when they have to export solids manually from the File menu to ACIS SAT format.
I acheived the task but because I am new to AutoCAD it's very ugly and seems inefficient. I'd like to ask for some help to find a nicer .NET solution to this:
First of all, I can find the solid I want using .NET API with this function:
Public Function GetVisible3dSolid(Optional ByVal Visible As Boolean = True) As Object
Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction = db.TransactionManager.StartTransaction()
For i As Long = db.BlockTableId.Handle.Value To db.Handseed.Value - 1
Dim id As ObjectId = ObjectId.Null
Dim h As New Handle(i)
If db.TryGetObjectId(h, id) Then
If id.ObjectClass.Name = "AcDb3dSolid" Then
Dim ent As Entity = CType(tr.GetObject(id, OpenMode.ForRead, True), Entity)
If ent.Visible = Visible Then
tr.Commit()
Return id
End If
End If
End If
Next
tr.Commit()
End Using
Return Nothing
End Function
But I can't find a good .NET solution to how to select this ObjectID in a AcadSelectionSet.
I ended up coding it up in COM: I select AcSelect.acSelectionSetAll and when I loop through the selection I place everything in a removeObjects list EXCEPT the solid I just found above. Then I call RemoveItems on my AcadSelectionSet to remove everything EXCEPT my solid. What I would want ideally is to use AddItems to do the opposite: add my solid as the only selected object and then call Export. Is there a way to do this (if possible using .NET)
Here is my current code:
Public Function ExportDWG2SAT(ByVal lsFullName As String) As Object
Dim ssetObj As AcadSelectionSet
Dim removeObjects As New List(Of AcadEntity)
Dim sImage_Type = "SAT"
ssetObj = m_currentDoc.SelectionSets.Add("SSALL") ' creates named selection set
ssetObj.Select(AcSelect.acSelectionSetAll) 'select everything in the drawing
Dim my3DSolidObj As Object = GetVisible3dSolid()
If Not IsNothing(my3DSolidObj) Then
Dim my3DSolid As ObjectId = CType(my3DSolidObj, ObjectId)
For Each ent As AcadEntity In ssetObj
Select Case ent.ObjectName
Case "AcDb3dSolid"
If ent.ObjectID <> CType(my3DSolid.OldIdPtr, Long) Then
removeObjects.Add(ent)
End If
Case Else
removeObjects.Add(ent)
End Select
Next
Dim removeArr() As AcadEntity = removeObjects.ToArray
ssetObj.RemoveItems(removeArr)
m_currentDoc.Export(lsFullName, sImage_Type, ssetObj)
End If
Return Nothing
End Function