I am trying to figure out how to remove all the layers from a model using the VB API and coming up empty. I'm just starting with the API, but I have a small amount of VB.net experience. I think my biggest problem is figuring out how to define all the variables, and pick out objects that are layers. If anyone has done this, or something similar, and doesn't mind posting some of their code, I would greatly appreciate that. Thank you.
You might try the following. I had a function to blank all layers and modified it to delete.
This code is untested but should work.
Joe
'/*====================================================================*\
' Function : ProEVBDeleteAllLayers
' Purpose : Delete All of the Layers in a model
'\*====================================================================*/
Public Function ProEVBDeleteAllLayers(ByVal strModelName As String) As String
Dim descModel As IpfcModelDescriptor
Dim model As IpfcModel
If strModelName Is Nothing Or strModelName.Trim = "" Then
model = _asyncConnection.Session.CurrentModel
If model Is Nothing Then
Throw New Exception("MODEL_NOT_PRESENT")
End If
Else
'======================================================================
'Model is retrieved using a model descriptor object.
'This method loads the model identified by model type and path from a
'standard directory location.
'======================================================================
model = ProESession.GetModelFromFileName(strModelName)
If model Is Nothing Then
Dim options As IpfcRetrieveModelOptions
options = (New CCpfcRetrieveModelOptions).Create
options.AskUserAboutReps =False
descModel = (New CCpfcModelDescriptor).Create(ProEVBGetModelType(strModelName), Nothing, Nothing)
descModel.Path = strModelName
model = ProESession.RetrieveModelWithOpts(descModel, options)
End If
End If
Dim myModelItems As IpfcModelItems
myModelItems =CType(model, IpfcModelItemOwner).ListItems(EpfcModelItemType.EpfcITEM_LAYER)
Dim myLayer As IpfcLayer
For iLayer As Integer = 0 To myModelItems.Count - 1
myLayer = myModelItems.Item(iLayer)
Try
myLayer.Delete()
Catch ex As Exception
Return "ProEVBDeleteAllLayers: " & CType(myLayer, IpfcModelItem).GetName & " : " & ex.Message
End Try
Next
Return "PRO_TK_NO_ERROR"
End Function
Thank you, I actually ended up with something very similar (I managed to get somewhere with it). I didn't test your code, but other than the error checking you had in the code I noticed you put the items from the sequence of model items (that happened to be layers) directly to an IpfcLayer. I want to be able to read the names of the layers at a later time (when I get further along in programming this) and don't think I can get the name from the ipfcLayer, so I made it a modelitem first.
Maybe you can help me with something else as well. I'm trying to put items into new layers and tried doing a listitems on the same modelitemowner, but without a type. VB is telling me that it is not an optional argument, so I put in Null like I'd found in the user guide, and it's still not putting any items into the sequence I'm trying to put them in. Thanks again for posting the code up.
retrieve model
Dim model As IpfcModel
Set model = session.CurrentModel
Dim IndexItems As IpfcModelItems
Dim indexlayer As ipfcmodelitem
Dim miowner As IpfcModelItemOwner
Dim id As Long
Dim layer As IpfcLayer
Set miowner = model
'put all layers in a sequence
Set IndexItems = miowner.ListItems(EpfcITEM_LAYER)
Sheets("sheet1").Range("b5").Value = IndexItems.Count
'delete layers
For id = 0 To IndexItems.Count - 1
Set indexlayer = IndexItems.item(id)
Sheets("sheet1").Range("a6").Value = indexlayer.GetName()
Set layer = indexlayer
layer.Delete
Next
I figured out the problem with null, I am supposed to use EpfcModelItemType_nil to represent that I dont want any particular type.
I added some code. I used EpfcModelItemType_FEATURE because the EpfcModelItemType_nil returned all things and some of them are not valid on a layer.
'/*====================================================================*\
' Function : ProEVBAddAllItemsToLayer
' Purpose : Add All Items to the Layers in a model
'\*====================================================================*/
Public Function ProEVBAddAllItemsToLayer(ByVal strModelName As String, ByVal strLayerToAddItemsTo As String) As String
Dim descModel As IpfcModelDescriptor
Dim model As IpfcModel
If strModelName Is Nothing Or strModelName.Trim = "" Then
model = _asyncConnection.Session.CurrentModel
If model Is Nothing Then
Throw New Exception("MODEL_NOT_PRESENT")
End If
Else
'======================================================================
'Model is retrieved using a model descriptor object.
'This method loads the model identified by model type and path from a
'standard directory location.
'======================================================================
model = ProESession.GetModelFromFileName(strModelName)
If model Is Nothing Then
Dim options As IpfcRetrieveModelOptions
options = (
New CCpfcRetrieveModelOptions).Create
options.AskUserAboutReps =
False
descModel = (
New CCpfcModelDescriptor).Create(ProEVBGetModelType(strModelName), Nothing, Nothing)
descModel.Path = strModelName
model = ProESession.RetrieveModelWithOpts(descModel, options)
End If
End If
Try
model.CreateLayer(strLayerToAddItemsTo)
Catch ex As Exception
End Try
Dim myModelItems As IpfcModelItems
myModelItems =
CType(model, IpfcModelItemOwner).ListItems(EpfcModelItemType.EpfcITEM_LAYER)
Dim myLayer As IpfcLayer
For iLayer As Integer = 0 To myModelItems.Count - 1
myLayer = myModelItems.Item(iLayer)
If myModelItems.Item(iLayer).GetName().ToString.Trim.ToUpper = strLayerToAddItemsTo.Trim.ToUpper Then
Dim myAllModelItems As IpfcModelItems
myAllModelItems =CType(model, IpfcModelItemOwner).ListItems(EpfcModelItemType.EpfcModelItemType_FEATURE)
For MyLayerItem As Integer = 0 To myAllModelItems.Count - 1
Try
myLayer.AddItem(myAllModelItems.Item(MyLayerItem))
Catch ex As Exception
'Return "ProEVBAddAllItemsToLayer: " & ex.Message
End Try
Next
End If
Next
Return "PRO_TK_NO_ERROR"
End Function
Thanks for the replies. It's been awhile as I am doing this in between other work. I do believe that would work to add all the items to a layer, but I'm trying to be more specific with the addition of items. I'm trying to use the Visual Basic API with VBA, through excel, to add features to layers, so I've noticed that some of the coding you're using doesn't quite work. I still apprecitate the help as it's the same basic coding, but things like the try...catch function don't work, and .toupper requires a different method.
For simplicity, lets say I want to add all the datum planes to my layer ALL_PLANES.
This is what I have
Dim datumplanelist As IpfcFeatures
Dim addfeature As ipfcmodelitem
Dim pl As Long
'Put datum plane features into a sequence
Set datumplanelist = Solid.ListFeaturesByType(False, EpfcFEATTYPE_DATUM_PLANE)
'run through each of the items from datumplanelist and add to layer ALL_PLANES
For pl = 0 To datumplanelist.Count - 1
Set addfeature = datumplanelist.item(pl)
'**problem HERE \/
Planes.AddItem (addfeature)
Next
It is giving me that the addfeature variable is throwing the compile error "Method or data member not found"
it does the same thing if addfeature is defined as IpfcFeature
Also, I defined solid like this:
Set Model = session.CurrentModel
Set Solid = CType(Model, IpfcSolid)
Thanks