Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Delete occurrence in Assembly

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
filippo.costantin3HZMT
393 Views, 8 Replies

Delete occurrence in Assembly

Hi everyone, 

 

I wrote a code for read iproperties from part or assembly in folder. The code run:

1 - read all file in folder

2 - filter file in folder (.asm, .ipt)

3 - add component to assembly

4 - read iproperties

5 - delete component

All works very weel but the last phase give me the error:

filippocostantin3HZMT_0-1666341928679.png

I create many logger for control the code anche the code raw that give me a problem is:

 

Sub DeleteComponent()
	Dim oAsmCompDef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition
	Dim oOcc As ComponentOccurrence
	For Each oOcc In oAsmCompDef.Occurrences
		
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
			Logger.Info("⃝⃝⃝⃝  [ Rimozione assieme " & oOcc.Name & " ]")
			oOcc.Delete()
			Logger.Info("⃝⃝⃝⃝  [ Fine rimozione dell'elemento  " & oOcc.Name & " ]")
		End If	
	Next
End Sub

 

oOcc.Delete()

 

I've already use this function in other code but now I've this problem.

 

How can i Solve it? 

 

In attached the complete code. 

 

Thanks

8 REPLIES 8
Message 2 of 9

Hi @filippo.costantin3HZMT.  I can't read what the error message says, because it is not in English, but it kind of looks like "Unspecified Error", which would not be that helpful anyways.  The second tab of the error message dialog sometimes has more useful information somewhere within it, but not always.  Are you sure it is that 'oOcc.Delete' line causing the error?  I don't know if it would be any different functionality, but the first alternative I might have tried would be oOcc.Delete2(True).  The 'True' there tells it to skip saving the source document of the component.  Another thing that comes to mind is that the assembly may need to be updated, because maybe it is trying to delete a component that no longer exists or something like that, though it seems a bit unlikely.  It was rather difficult to follow along with your code, due to the language difference, and the amount of commented out stuff remaining within it, but it kind of looks like you may be adding an assembly type component to the 'active' assembly, then almost immediately deleting it again, without any document update of any sort in between the two.  If that is true, what is the purpose...just curiosity.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 9

Hi, Yes It's the workflow, I need to open, read iporperties and close file. After this simple action i want to create a list with all iproperty that I extract from file and write on excel. 

This is the specification of error:

filippocostantin3HZMT_0-1666354100425.png

 

Message 4 of 9

Hi @filippo.costantin3HZMT.  If you just need to open the document so you can read or edit its iProperty, then close it again, there is an easier, and more logical way to do that.

Instead of:

 

Call AddComponent(file)

 

...use this:

 

Dim oFileDoc As Document = ThisApplication.Documents.Open(file, False)

 

...then, instead of this:

 

Call DeleteComponent()

 

 ...use this:

 

oFileDoc.Close(True) 'True = skip save

 

Or, instead of the oFileDoc.Close, you might also be able to use the following routine:

 

oFileDoc.ReleaseReference

 

...but then, after the end of the loop, if you used the ReleaseReference method, you can use this:

 

ThisApplication.Documents.CloseAll(True) 'True  = only close all unreferenced documents

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 9

Thanks for your advice. But how can I save the iProperties in a list if I open every file?

Message 6 of 9

It looks like you are creating a new String type variable between those two processes called "Codice".  Then you are setting its value from that file's custom iProperty.  Then you are writing that value out to the iLogic Logger.  If you do not intend to copy data from the Logger, and you do not want to inject a call to a custom Sub routine there that would write that value directly into an Excel sheet, then I would suggest that you create something like a New List(Of String) type variable somewhere before this point in your code (before the loop), then 'Add' this custom iProperty value to that List between those two processes.  Then later, after the loop, you will have that List object filled with those values that you can do what you want with.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 9

How can I read the iproperties after use 

Dim oFileDoc As Document = ThisApplication.Documents.Open(file, False)

I can't read because the rule find the iProperties in the assembly where I launch the rule.

Message 8 of 9

Good point.  That iLogic only shortcut snippet 'iProperties.Value()' is limited to only being able to access the iProperties of documents that are referenced by the 'active' document.  So, in this case, I would suggest using the API route to access that document's iProperty.

Dim oFileDoc As Document = ThisApplication.Documents.Open(file, False)
Dim Codice As String
Codice = oFileDoc.PropertySets.Item("Inventor User Defined Properties").Item("PR_Codice").Value

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 9

It works very well. Thanks!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report