Hiding parts using an excel spreadsheet

Hiding parts using an excel spreadsheet

Anonymous
Not applicable
580 Views
2 Replies
Message 1 of 3

Hiding parts using an excel spreadsheet

Anonymous
Not applicable

See code - I am missing something that can get the "part" which is a member of the "parts" range sent to the private sub with the false modifier.

 

I get an error that says, "object required."

It's some kind of basic syntax thing, provided everything else is working.

 

I've attached the spreadsheet I'm using - it's had some editing, as I get better I want to use these as they come from our client.

 

 

Sub HideParts()
Dim oApp As Application
Set oApp = ThisApplication

Dim oDoc As Document
Set oDoc = oApp.ActiveDocument

' Check if user is in part or assembly
If oDoc Is Nothing Or TypeOf oDoc Is DrawingDocument Then
    Exit Sub
End If

Dim oCompDef As ComponentDefinition
' Get component definition
Set oCompDef = oDoc.ComponentDefinition

'Dim oParams As Parameters
'Dim oParam As Parameter
' Get parameters object (JG: don't need this?)
'Set oParams = oCompDef.Parameters

' define wsheet variable, etc
Dim exapp As Excel.Application
Dim wsheet As Excel.WorkSheet
Dim part As Excel.Range
Dim parts As Excel.Range
'What does this do?
    'It sucks in air
        'It's definitely sucking...
Dim pt(2) As Double
        
Set exapp = CreateObject("Excel.Application")

'this actually works to set the path:
Dim sFilePath
sFilePath = exapp.GetOpenFilename(FileFilter:="Excel Files (*.xls*),*.xls*", Title:="Select Work Order")

'pop open excel visibly so I know I have the right file and workbook open (for now...)
exapp.Visible = True
exapp.Workbooks.Open sFilePath
Set wsheet = exapp.ActiveSheet

'Seems reasonable
Set parts = wsheet.Range("I2:I87")

parts.Select

pt(0) = 0: pt(1) = 0: pt(2) = 0

For Each part In parts

Call SetVisibility(oAsmDoc.ComponentDefinition.Occurrences, part.Text, False)

pt(0) = pt(0) + 1
pt(1) = pt(1) + 1

Next part


End Sub

Private Sub SetVisibility(Occurrences As ComponentOccurrences, SearchName As String, VisibilityOn As Boolean)

' Iterate through each of the occurrences in the collection provided.

Dim oOccurrence As ComponentOccurrence

For Each oOccurrence In Occurrences

' Check to see if the occurrence name matches the search name.

' The strings are converted to upper case to remove context sensitivity.

If UCase(oOccurrence.Name) Like UCase(SearchName) Then

' Check to see if the visibility is different than the specified visiblity.

If oOccurrence.Visible <> VisibilityOn Then

' Set the visibility of the occurrence.

oOccurrence.Visible = VisibilityOn

End If

End If



' If this occurrence is a subassembly, recursively call this

' function to traverse through the subassembly.

If oOccurrence.DefinitionDocumentType = kAssemblyDocumentObject Then

Call SetVisibility(oOccurrence.SubOccurrences, SearchName, VisibilityOn)

End If

Next

End Sub



Thanks in advance if anyone can help me out!

 

James

 

 

 

 

0 Likes
Accepted solutions (1)
581 Views
2 Replies
Replies (2)
Message 2 of 3

ekinsb
Alumni
Alumni
Accepted solution

I did a quick test of your code and found one error.  You reference a variable named "oAsmDoc" where you call the SetVisibility function in HideParts.  That variable is not declared anywhere.  I changed it to oDoc and it seemed to run ok after that.

 

The other thing that I don't know about because I don't have your assembly, is if the names in the spread sheet exactly match the names in the assembly.  For example, typically occurrences will be the name of the assembly with a colon and a number.  For example, if the spread sheet has the id "171292" but the occurrences are named "171292:1", "171292:2", and "171292:3", it won't find them.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3

Anonymous
Not applicable

Hi, Brian!  

I thought of checking for those last night, this is several different routines mashed together - thanks for the help!


In the routine that actually hides the parts based on input, wildcards work, so I will probably just put them in the spreadsheet for now and probably add them to the code in the future.  Should be easy enough to build strings, but I'm very new.

 

IT WORKS!  Man is this neat! 😄

 

0 Likes