New problem with existing code - no ComponentDefenition

New problem with existing code - no ComponentDefenition

chrisw01a
Collaborator Collaborator
237 Views
4 Replies
Message 1 of 5

New problem with existing code - no ComponentDefenition

chrisw01a
Collaborator
Collaborator

Hey all,

I've used this iLogic for a long time. Not sure why its not working now. It's stopping on line 37 because oDoc does not contain a "ComponentDefenition" property. Any clue how I fix this? Thanks!

 

chrisw01a_0-1733329850425.png

 

 

 

 

'verify document type
If ThisApplication.ActiveDocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
	'MessageBox.Show("Document Type: " & ThisApplication.ActiveDocumentType.ToString)
	MessageBox.Show("Must be run from an Assembly! Program will now exit.")
	Exit Sub
End If

'get current colorcheme name 
'oColorScheme = ThisApplication.ActiveColorScheme.Name

'get current colorcheme background type
'oBackGroundType = ThisApplication.ColorSchemes.BackgroundType

'Change to Presentation (white background)
'ThisApplication.ColorSchemes.Item("Presentation").Activate

'set to use one color background type
'ThisApplication.ColorSchemes.BackgroundType = _
'BackgroundTypeEnum.kOneColorBackgroundType 


Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
Dim oDoc As Document
Dim oRefFile As FileDescriptor

'set iproperty to use ISO view on save
	oAsmDoc.SetThumbnailSaveOption _
	(ThumbnailSaveOptionEnum.kActiveComponentIsoViewOnSave)

For Each oDoc In oAsmDoc.AllReferencedDocuments
	ThisApplication.Documents.Open(oDoc.FullFileName, True)
	
	'MessageBox.Show("Processing: " & oDoc.FullDocumentName)
	
	For Each oSketch In oDoc.ComponentDefinition​.Sketches
		oSketch.Visible = False
	Next
	
	'set work plane visibility
	For Each oWorkPlane In oDoc.ComponentDefinition.WorkPlanes
	oWorkPlane.Visible = False
	Next
	
	'set work axis visibility
	For Each oWorkAxis In oDoc.ComponentDefinition.WorkAxes
	oWorkAxis.Visible = False
	Next
	
	'set work point visibility
	For Each oWorkPoint In oDoc.ComponentDefinition.WorkPoints
	oWorkPoint.Visible = False
	Next

	'set iproperty to use ISO view on save
	oDoc.SetThumbnailSaveOption _
	(ThumbnailSaveOptionEnum.kActiveComponentIsoViewOnSave)
	
	'save and close the file
	oDoc.Save
	oDoc.Close
Next

'Change back to original scheme
'ThisApplication.ColorSchemes.Item(oColorScheme).Activate 

'Change back to origina back ground type
'ThisApplication.ColorSchemes.BackgroundType = oBackGroundType

 

 

 

0 Likes
238 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @chrisw01a.  Interesting and odd error.  It seems like some folks will sometimes encounter unexpected items when iterating through the AllReferencedDocuments collection, such as derived-in references, and others.  Not sure if that has anything to do with it or not though.

I might suggest a few slight alterations to the code, which may not seem important, but might possibly make a difference in an odd situation like this, so they may be worth trying.  Below is a copied, and slightly altered version of your original code, that should still do the same task, the same way.  See if this makes any difference.

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	'try to set active document as value of AssemblyDocument type variable, without throwing error
	Dim oAsmDoc As AssemblyDocument = TryCast(oInvApp.ActiveDocument, AssemblyDocument)
	'if it was not an assembly, then that variable did not get a value set to it
	If oAsmDoc Is Nothing Then
		MessageBox.Show("Must be run from an Assembly! Program will now exit.")
		Exit Sub
	End If
	'get current colorcheme name 
	'oColorScheme = oInvApp.ActiveColorScheme.Name
	'get current colorcheme background type
	'oBackGroundType = oInvApp.ColorSchemes.BackgroundType
	'Change to Presentation (white background)
	'oInvApp.ColorSchemes.Item("Presentation").Activate
	'set to use one color background type
	'oInvApp.ColorSchemes.BackgroundType = _
	'BackgroundTypeEnum.kOneColorBackgroundType 
	Dim oRefFile As FileDescriptor
	'set iproperty to use ISO view on save
	oAsmDoc.SetThumbnailSaveOption _
	(ThumbnailSaveOptionEnum.kActiveComponentIsoViewOnSave)
	For Each oDoc As Inventor.Document In oAsmDoc.AllReferencedDocuments
		Dim oDocToUse As Inventor.Document = oInvApp.Documents.Open(oDoc.FullDocumentName, True)
		Dim oCD As ComponentDefinition = Nothing
		Try
			oCD = oDocToUse.ComponentDefinition
		Catch
			MessageBox.Show("Could Not Get ComponentDefinition Of Following Referenced Document" _
			& vbCrLf & oDocToUse.FullDocumentName, "No ComponentDefinition", MessageBoxButtons.OK, _
			MessageBoxIcon.Exclamation)
			Continue For
		End Try
		For Each oSketch As PlanarSketch In oCD.Sketches
			oSketch.Visible = False
		Next
		'set work plane visibility
		For Each oWorkPlane As WorkPlane In oCD.WorkPlanes
			oWorkPlane.Visible = False
		Next
		'set work axis visibility
		For Each oWorkAxis As WorkAxis In oCD.WorkAxes
			oWorkAxis.Visible = False
		Next
		'set work point visibility
		For Each oWorkPoint As WorkPoint In oCD.WorkPoints
			oWorkPoint.Visible = False
		Next
		'set iproperty to use ISO view on save
		oDocToUse.SetThumbnailSaveOption _
		(ThumbnailSaveOptionEnum.kActiveComponentIsoViewOnSave)
		'save and close the file
		oDocToUse.Save()
		oDocToUse.Close(True) 'True means skip save
	Next
	'Change back to original scheme
	'oInvApp.ColorSchemes.Item(oColorScheme).Activate 
	'Change back to origina back ground type
	'oInvApp.ColorSchemes.BackgroundType = oBackGroundType
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 5

chrisw01a
Collaborator
Collaborator

Thank you for writing this. This does not seem to work either. I have attached the assembly files here along with the error. Even though it is only failing on "-0NEST.iam", none of the other files that it opened seem to have had their thumbnails updated.

Capture.PNG

Capture2.PNG

Capture3.PNG

  

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

Well, at least that looks like a different error than before.  Now, instead of it not being able to find the ComponentDefinition of a PartDocument, it appears to be encountering an error while trying to 'set' the visibility of a WorkPoint.

WCrihfield_0-1733757963322.png

That could be seen as progress, I guess, even though it is not error free yet.

It is also interesting that, in order to get to the WorkPoint stage of the process, it would have had to get through getting the ComponentDefinition, then setting visibility of sketches, WorkPlanes, and WorkAxes first, then on to the WorkPoints.  If this were a simple Read/Write related permission issue, I would have expected it to fail before that point.  With that in mind, I still am not sure what is causing this new error.  I am wandering if it may have something to do with the DVR's (DesignViewRepresentations), either at the part (referenced document) level, or at the parent assembly level.  If the current/active DVR in the referenced document is 'locked', then maybe it does not like changing visibility of things, because those changes can not be recorded, or something like that.  Or, if the component occurrence in the assembly is set to a specific DVR, and that is also set as 'associative' (so that visual changes to the referenced document immediately effect that occurrence in the assembly), then changing visibility settings will often 'break' that associative setting.  When doing things like that manually, that will often show a warning dialog, warning that your actions will break that associative setting, and offering an alternative option, or the opportunity to reverse your decision before it takes place.  That may (or may not) be throwing the exception, I can not be sure.  Stuff like that can be complicated to manage fluidly & intuitively by code.

 

I can not use your attached ZIP file, due to security policies where I work, so hopefully others will also look into this for you also.  Good luck.

By the way, if all else fails, I would highly recommend including one main, or multiple smaller, Try...Catch...End Try statements within your code routine.  That will let you 'handle' the potential / expected errors, while letting your code routine continue to run past where those errors may get encountered.  Put the line(s) of code that you expect might fail on the 'Try' side, then optionally put something else on the 'Catch' side, which will only run when the code on the Try side encounters an error.  However, do not put something on the Catch side that might also possibly cause an error or fail.  If you put nothing on the catch side, or only write something to the iLogic Log window on that side, then your code will simply continue to run, past where it encounters those errors, with that one error producing task not having been done, but others will get done.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

chrisw01a
Collaborator
Collaborator
It just doesn't make any sense that Autodesk would remove the "ComponentDefenition" property from Document. Any from Autodesk here?
0 Likes