Save all idw's in same folder to dwg

Save all idw's in same folder to dwg

Frank-Oosterwaal
Collaborator Collaborator
743 Views
7 Replies
Message 1 of 8

Save all idw's in same folder to dwg

Frank-Oosterwaal
Collaborator
Collaborator

Hi everyone,

 

I'm running underneath code from a form which can be accessed from a drawing. It will create a dwg of all idw's in the same folder as where the drawing is. It works but there are two things that go wrong.

 

  1. See attachment. In the dwg folder where it creates the dwg's it sometimes leaves the .bak file and when it does that the letters in the filename (part number) are not in capitals. It sometimes does it with one file as in the attachment but it can also do that with more than one file.
  2. After running this rule and closing the drawing (all Inventor files are closed) Inventor thinks there's still a file open. So when I try to change the project file Inventor says: 'The active project cannot be changed while Inventor files are open.'

I have a similar code running for making pdf's of all idw's and running this code has the same project file problem.

 

A different question I have is about the creation of the dwg files. It looks like the DWGAddIn is started and stopped with each file save. Is there a way to avoid this and speed up the process?

 

Any ideas? Thanks in advance!

 

This is the code:

SyntaxEditor Code Snippet

'-----Check path----- 
oPath = ThisDoc.Path 

'-----get DWG target folder path-----
oDWGFolder = oPath & "\" & "DWG"

'-----Check for the DWG folder and create it if it does not exist-----
If Not System.IO.Directory.Exists(oDWGFolder) Then
System.IO.Directory.CreateDirectory(oDWGFolder)
End If

'-----Search for idw's in folder-----
Dim MyFiles As String()
    
    MyFiles = System.IO.Directory.GetFiles(oPath, "*.idw")
    
'-----Starts the Loop-----
For Each MyFile As String In MyFiles


        ThisApplication.SilentOperation = True
        Dim partDoc As Document = ThisApplication.Documents.Open(MyFile, False)
        
        On Error Resume Next

If partDoc.DocumentType = kDrawingDocumentObject Then
	
            Dim oPropSet As PropertySet
            Dim oProp As Inventor.Property
            Dim invPartNoProperty As Inventor.Property = partDoc. _
            PropertySets.Item("Design Tracking Properties").Item("Part Number")
            PartNumber = invPartNoProperty.Value                   
            oRevNum = iProperties.Value("Project", "Revision Number")	
	

Dim DWGAddIn As TranslatorAddIn
	DWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
	Dim oDocument As Document
	oDocument = ThisApplication.ActiveDocument
	
	Dim oContext As TranslationContext
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = kFileBrowseIOMechanism

    ' Create a NameValueMap object
    Dim oOptions As NameValueMap
    oOptions = ThisApplication.TransientObjects.CreateNameValueMap

    ' Create a DataMedium object
    Dim oDataMediumDWG As DataMedium
    oDataMediumDWG = ThisApplication.TransientObjects.CreateDataMedium




'-----Filename definition-----
oFileName = oDWGFolder & "\" & PartNumber & "r" & oRevNum & ".dwg"

oDataMediumDWG.FileName = oFileName


'-----Save to DWG-----
DWGAddIn.SaveCopyAs(partDoc, oContext, oOptions, oDataMediumDWG)

End If

 partDoc = Nothing
  ThisApplication.SilentOperation = False


Next

 

---------------------------------------------------------------------------------------------------------
0 Likes
744 Views
7 Replies
Replies (7)
Message 2 of 8

bshbsh
Collaborator
Collaborator

For #2: I'm not at work right now so can't check, but there was an api call somewhere to release a document, maybe try that.

And why do you redefine the DWGAdding inside the for loop, for each file? Can you not just put it outside the loop and just do it once?

Message 3 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@Frank-Oosterwaal,

 

  1. See attachment. In the dwg folder where it creates the dwg's it sometimes leaves the .bak file and when it does that the letters in the filename (part number) are not in capitals. It sometimes does it with one file as in the attachment but it can also do that with more than one file. - For more details on .bak file, go through below link - https://knowledge.autodesk.com/support/autocad/learn-explore/caas/sfdcarticles/sfdcarticles/Understa...
  2. After running this rule and closing the drawing (all Inventor files are closed) Inventor thinks there's still a file open. So when I try to change the project file Inventor says: 'The active project cannot be changed while Inventor files are open.' -

     

    Before you change the active project, close all Autodesk Inventor files.

    1. Select   Manage  Projects.
    2. In the Project dialog box, at the top, select a project.
    3. Click Apply.

      The selected file becomes active.

    Note: If a project is not listed, click Browse. In the Choose Project File dialog box, search for it. The project is located and is added to the list.

Hoping that iLogic code would help to save .idw to .dwg file.

 

'-----Check path----- 
oPath = ThisDoc.Path 

'-----get DWG target folder path-----
oDWGFolder = oPath & "\" & "DWG"

'-----Check for the DWG folder and create it if it does not exist-----
If Not System.IO.Directory.Exists(oDWGFolder) Then
System.IO.Directory.CreateDirectory(oDWGFolder)
End If

'-----Search for idw's in folder-----
Dim MyFiles As String()
    
    MyFiles = System.IO.Directory.GetFiles(oPath, "*.idw")
    
'-----Starts the Loop-----
For Each MyFile As String In MyFiles


        ThisApplication.SilentOperation = True
        Dim partDoc As Document = ThisApplication.Documents.Open(MyFile, False)
        
        On Error Resume Next

		If partDoc.DocumentType = kDrawingDocumentObject Then
			
		            Dim oPropSet As PropertySet
		            Dim oProp As Inventor.Property
		            Dim invPartNoProperty As Inventor.Property = partDoc. _
		            PropertySets.Item("Design Tracking Properties").Item("Part Number")
		            PartNumber = invPartNoProperty.Value                   
		            oRevNum = iProperties.Value("Project", "Revision Number")	 

					'-----Filename definition-----
					oFileName = oDWGFolder & "\" & PartNumber & "r" & oRevNum & ".dwg"

					partDoc.SaveAs(oFileName, True) 

		End If

 		partDoc = Nothing
 		ThisApplication.SilentOperation = False


Next

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 4 of 8

Frank-Oosterwaal
Collaborator
Collaborator

Hi again,

 

First of thank you for your answers and I'm not pressing the button need an answer. This happens to allot of my posts without me doing anything...

 

@bshbsh: Putting the DWGaddin outside of the loop makes allot of sence! Thanks for that, it speeds up the process and it also seems to solve the .bak problem! The project file problem however I've not been able to solve yet. Did not find the right code yet that releases the document. Any ideas?

 

@chandra.shekar.g: I know how to choose another project file, that's not the problem. The problem is that you can't select another project file because Inventor thinks there's still a file open, which there is not. 

 

Thanks again for your help and if anyone has an idea how to solve the project file problem please let me know!

 

Greets,

Frank

---------------------------------------------------------------------------------------------------------
0 Likes
Message 5 of 8

Frank-Oosterwaal
Collaborator
Collaborator

Hi all,

I also noticed that the code is wrong because it only uses the revision number of the drawing the code is running from. I'm guessing I have to fetch that from the property set, just as the part number but I can't figure out how to do that. I tried to add it as:

 

SyntaxEditor Code Snippet

	If partDoc.DocumentType = kDrawingDocumentObject Then
    	Dim oPropSet As PropertySet
		Dim oProp As Inventor.Property
		Dim invPartNoProperty As Inventor.Property = partDoc. _
		PropertySets.Item("Design Tracking Properties").Item("Part Number")
		PartNumber = invPartNoProperty.Value
		Dim invRevNoProperty As Inventor.Property = partDoc. _
		PropertySets.Item("Design Tracking Properties").Item("Revision Number")
		oRevNum = invRevNoProperty.Value

but that results in no revision at all in the filename. I tried to look for the answer but I only find stuff about getting the part number this way. Somebody know how to get the revision number?

 

Thanks in advance! 

---------------------------------------------------------------------------------------------------------
0 Likes
Message 6 of 8

Frank-Oosterwaal
Collaborator
Collaborator

Sorry, I see that I used the wrong property set. Changed it to 'Inventor Summary Information' and it works. So only the question about the project file remains.

---------------------------------------------------------------------------------------------------------
0 Likes
Message 7 of 8

bshbsh
Collaborator
Collaborator

Try to actually close the document:

partDoc.Close

 

And since you're opening them invisible, the ReleaseReference could be useful before the close as well.

0 Likes
Message 8 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@Frank-Oosterwaal,

 

Before you change the active project, close all Autodesk Inventor files.

 

In your code, documents are opened in invisible state and remains in Inventor. So, all invisible documents need to close it.

 

As per @bshbsh suggestion, try to close all documents in Inventor.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes