Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to export parts 1

Status
Not open for further replies.

Jaani

Mechanical
Oct 8, 2013
20
I need to export all parts of a root product (document) as .stp. I want all points, lines, planes and such construction elemens to be hidden and then the export will be done. Unfortunately I have no knowledege about visual basic. Please may you help me out?
 
Replies continue below

Recommended for you

There you go. STP files will be saved in the same location where CATParts are coming from.

Code:
Sub CATMain()
    'Dim oPartDoc As PartDocument
    For i = 1 To CATIA.Documents.Count
        Set oPartDoc = CATIA.Documents.Item(i)
        If TypeName(oPartDoc) = "PartDocument" Then
            'dim oSelection as selection
            Set oSelection = oPartDoc.Selection
            oSelection.Search "(CATPrtSearch.AxisSystem.Visibility=Visible + (CATPrtSearch.Surface.Visibility=Visible + (CATPrtSearch.Wireframe.Visibility=Visible + CATPrtSearch.Sketch.Visibility=Visible))),all"
            oSelection.VisProperties.SetShow catVisPropertyNoShowAttr
            oPartDoc.ExportData oPartDoc.Path & "\" & oPartDoc.Product.PartNumber, "stp"
            oSelection.Clear
            Set oSelection = Nothing
        End If
    Next
End Sub
 
Like!

And if I want to export the seleted parts/subproducts only? Would you kindly guide me again?

Edit: I have noticed that the macro will export part regardless of deactivated status of its features. It will export part taking into account all features are activated. Please may you edit your code so that it exports the part as it is.
 
Hello,

There is a setting in Tools/Options/General/Compatibility/STEP related to NoShow entitites. I think that might work with deactivated features as well. Just make sure the checkbox is cleared.

Code:
Sub CATMain()
    'cancel alerts for now
    CATIA.DisplayFileAlerts = False
    
    'exit Sub if the active document is not a CATProduct
    If TypeName(CATIA.ActiveDocument) <> "ProductDocument" Then GoTo exitSub
    
    'initialize the selection
    Set oSelection = CATIA.ActiveDocument.Selection
    
    'exit Sub if there is nothing selected
    If oSelection.Count = 0 Then GoTo exitSub
    
    'create some sort of collection to hold the selected items
    'you could use Collection object instead of Scripting.Dictionary
    'depending on how you want this to run
    Set oDic = CreateObject("Scripting.Dictionary")
    
    For i = 1 To oSelection.Count
        'the assumption is that Products are selected and nothing else
        Set oSelectedObject = oSelection.Item(i).Value
        If TypeName(oSelectedObject) = "Product" Then
            oDic.Add i, oSelectedObject.ReferenceProduct.Parent
        End If
    Next
    
    'check again the validity of selected elements and exit if
    'your custom collection is empty (i.e. no proper element has been selected)
    If oDic.Count = 0 Then GoTo exitSub
    
    'search the selection for elements to hide
    oSelection.Search "(CATPrtSearch.AxisSystem.Visibility=Visible + (CATPrtSearch.Surface.Visibility=Visible + (CATPrtSearch.Wireframe.Visibility=Visible + CATPrtSearch.Sketch.Visibility=Visible))),all"
    oSelection.VisProperties.SetShow catVisPropertyNoShowAttr
    oSelection.Clear
    
    'loop inside your custom collection and export each document.
    For i = 1 To oDic.Count
        Set oProdDoc = oDic.Item(i)
        oProdDoc.ExportData oProdDoc.Path & "\" & oProdDoc.Product.PartNumber, "stp"
    Next
    
    'Exit sub
exitSub:
    CATIA.DisplayFileAlerts = True
End Sub
 
Am I doing anything wrong?
2018-04-17_13-44-12_bynman.png
 
Code:
Sub CATMain()
    'cancel alerts for now
    CATIA.DisplayFileAlerts = False
    
    'exit Sub if the active document is not a CATProduct
    If TypeName(CATIA.ActiveDocument) <> "ProductDocument" Then Exit Sub
    
    'initialize the selection
    Set oSelection = CATIA.ActiveDocument.Selection
    
    'exit Sub if there is nothing selected
    If oSelection.Count = 0 Then Exit Sub
    
    'create some sort of collection to hold the selected items
    'you could use Collection object instead of Scripting.Dictionary
    'depending on how you want this to run
    Set oDic = CreateObject("Scripting.Dictionary")
    
    For i = 1 To oSelection.Count
        'the assumption is that Products are selected and nothing else
        Set oSelectedObject = oSelection.Item(i).Value
        If TypeName(oSelectedObject) = "Product" Then
            oDic.Add i, oSelectedObject.ReferenceProduct.Parent
        End If
    Next
    
    'check again the validity of selected elements and exit if
    'your custom collection is empty (i.e. no proper element has been selected)
    If oDic.Count = 0 Then Exit Sub
    
    'search the selection for elements to hide
    oSelection.Search "(CATPrtSearch.AxisSystem.Visibility=Visible + (CATPrtSearch.Surface.Visibility=Visible + (CATPrtSearch.Wireframe.Visibility=Visible + CATPrtSearch.Sketch.Visibility=Visible))),all"
    oSelection.VisProperties.SetShow catVisPropertyNoShowAttr
    oSelection.Clear
    
    'loop inside your custom collection and export each document.
    For i = 1 To oDic.Count
        Set oProdDoc = oDic.Item(i)
        oProdDoc.ExportData oProdDoc.Path & "\" & oProdDoc.Product.PartNumber, "stp"
    Next
    
End Sub
 
Although the no show check box is cleared but the macro still exports part with features activated. However, I'm really much appreciated for your nice code, cilici. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor