Changing projects; VBA vs. manual

Changing projects; VBA vs. manual

mr_ensing
Advocate Advocate
2,596 Views
19 Replies
Message 1 of 20

Changing projects; VBA vs. manual

mr_ensing
Advocate
Advocate

Summary: I find that changing the project (*.ipj) within a VBA macro results in errors that don't occur when i change manually (by way of the Project manager).

 

The situation:

  • For every client project there is a unique Inventor project file;
  • A collection of 'standards', assy with drawing, to be copied to a project and modified there;
  • Inventor (and Vault Basic) 2015.

I have a elaborate 'copy design' macro (in VBA). The macro:

  • copies a drawing or assembly (including referenced document) from one project to another;
    renames all the files according to new project and subject;
  • edits iproperties of files according new project and subject;
  • replaces ReferencedFileDescriptors to new filenames and paths;
  • all the copying and replacing is done within a single-user project-file that excists for this purpuse.

Global workflow:

  1. So the user starts within a project and target design loaded,
  2. User starts the 'copy design' macro,
  3. User is asked to input target project and subject,
  4. Macro activates a single-user project-file (copy_object.ipj),
  5. Copying and replacing of references takes place,
  6. Macro unloads everyting, activates target project-file and opens result.

Two problems arise, both of wich i attribute to the changing of project files in VBA:

  1. The replacing of ReferencedFileDescriptors goes haywire.
    • This only happens if i leave the activating of the copy_object.ipj to the VBA macro itself.
    • When the user changes to copy_object.ipj before using the macro, not a single problem with the ReferencedFileDescriptors.
  2. At the end when the target project-file is activated and the result is opened, it is not possible to check the design in the Vault. Solution is to manually change to a random different project and back.

My conclusion is, that activating a project by way of VBA code gets me in some sort of project twilight zone.


My question, how to fix this?

 

I tried to isolate and clean up all the project-related code into a sub routine. No difference. Code:

 

Sub ChangeProj(aProj As String)

If ThisApplication.Documents.Count > 0 Then
    ThisApplication.Documents.CloseAll
End If

Dim aDesignProjectMgr As DesignProjectManager
Dim aProjectNew As DesignProject
Set aDesignProjectMgr = ThisApplication.DesignProjectManager

On Error Resume Next
Set aProjectNew = aDesignProjectMgr.DesignProjects.ItemByName(aProj)
If Err.Number <> 0 Then
    aDesignProjectMgr.DesignProjects.AddExisting (aProj)
    Set aProjectNew = aDesignProjectMgr.DesignProjects.ItemByName(aProj)
End If
Err.Clear
On Error GoTo 0

aProjectNew.Activate

End Sub

Manual changing of projects like this:

 

Project_manager.png

0 Likes
2,597 Views
19 Replies
Replies (19)
Message 2 of 20

rossano_praderi
Collaborator
Collaborator

Hi,
I've tried your code and for my testing I made some clean.

 

I've also added two lines of code that showing up the registered project names, in this way you can see the right name to use within your macro

 

Sub project()
    ChangeProj ("Inventor2016")
End Sub
Sub ChangeProj(aProj As String)

If ThisApplication.Documents.Count > 0 Then
    ThisApplication.Documents.CloseAll
End If

ThisApplication.DesignProjectManager.DesignProjects.ItemByName(aProj).Activate

End Sub

Sub Prjlist()
For Each adis In ThisApplication.DesignProjectManager.DesignProjects
    Debug.Print adis.Name
Next
End Sub

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 3 of 20

mr_ensing
Advocate
Advocate

I like how you cleaned up the code. I tried to get my code as minimal as possible. I do however need the AddExisting line for some situations, but for testing it does not matter.

 

Your Sub Prjlist() addition made me think about what string to input into this sub. I was parsing in the full path and filename ("C:/***/***/project.ipj").

So i tried to parse only the project name (basically the filename, without path and extension). This also works fine, but does not solve my problems.

 

For now i can only conclude that the VBA way of changing projects differs from the 'manual' way.

 

Thanks.

 

My current code:

 

 

Sub ChangeProj(aProj As String)

If ThisApplication.Documents.Count > 0 Then
ThisApplication.Documents.CloseAll
End If

On Error Resume Next 'optional code
ThisApplication.DesignProjectManager.DesignProjects.ItemByName(aProj).Activate
If Err.Number <> 0 Then 'optional code
ThisApplication.DesignProjectManager.DesignProjects.AddExisting (aProj) 'optional code
ThisApplication.DesignProjectManager.DesignProjects.ItemByName(aProj).Activate 'optional code
Err.Clear 'optional code
End If 'optional code
On Error GoTo 0 'optional code

End Sub

 

0 Likes
Message 4 of 20

mr_ensing
Advocate
Advocate

With further testing i isolated the second problem i have to a single line of code.


@mr_ensing wrote:

 

Two problems arise, both of wich i attribute to the changing of project files in VBA:

  1. ...
  2. At the end when the target project-file is activated and the result is opened, it is not possible to check the design in the Vault. Solution is to manually change to a random different project and back.

My conclusion is, that activating a project by way of VBA code gets me in some sort of project twilight zone.


Method 1:

  1. Start in project A,
  2. Use project dialog to ('manually') activate project B,
  3. Open a drawing, assy or part and try to check it in the Vault.

Method 2:

  1. Start in project A,
  2. Use test code to activate project B,
  3. Open a drawing, assy or part and try to check it in the Vault.

The line of code:

 

Public Sub test()
ThisApplication.DesignProjectManager.DesignProjects.ItemByName("I:\...\Project_B.ipj").Activate
End Sub

 

Method 1 just works.

 

With method 2 i get the following error:

 

checkin_error.png

 

If i click the Map Folders button end up in the project Twiligth Zone:

 

map_folders.png

 

 

0 Likes
Message 5 of 20

rossano_praderi
Collaborator
Collaborator

Hi Ensing,

I'm sorry for the late response, I been quitely busy in these days.

 

My last post was only for testing part of the final code.

 

Now the following code activate or create and activate a new project starting from an existing project template, that is locally.

 

Sub ChangeProj(ByVal aProj As String, ByVal pSource As String) 'work only with vault projects
Dim dProMan As DesignProjectManager
'Dim mProj As DesignProject ' optional code
Dim wPath As String ' workarea path

Set dProMan = ThisApplication.DesignProjectManager

wPath = Left(dProMan.ActiveDesignProject.WorkspacePath, _
        InStrRev(dProMan.ActiveDesignProject.WorkspacePath, "\"))

On Error Resume Next
If Dir(dProMan.DesignProjects.ItemByName(aProj).FullFileName) = "" Then
    'FileCopy pSource, wPath & aProj & ".ipj" ' all projects within the same folder (workarea)
    MkDir (wPath & aProj) ' make project folder, not needed if you use the workarea for all projects
    FileCopy pSource, wPath & aProj & "\" & aProj & ".ipj" ' each project with its folder
    dProMan.DesignProjects.AddExisting(wPath & aProj & "\" & aProj & ".ipj").Name = aProj
    dProMan.DesignProjects.ItemByName(aProj).VaultVirtualPath = _
        Replace(dProMan.DesignProjects.ItemByName(aProj).VaultVirtualPath, "Design", aProj)
' you can use the follow code if you need to check or adjust some project informations
'    Set mProj = dProMan.DesignProjects.ItemByName(aProj)
'    With mProj
'        .WorkspacePath = Replace(.WorkspacePath, "pSource", aProj) ' as an example
'        ....
'    End With
    ThisApplication.Documents.CloseAll
    dProMan.DesignProjects.ItemByName(aProj).Activate
Else
    ThisApplication.Documents.CloseAll
    dProMan.DesignProjects.ItemByName(aProj).Activate
End If

End Sub

 

This simple routine printout some project informations

Sub mProject(ByVal pName As String)
    Dim mP As DesignProject
    Set mP = ThisApplication.DesignProjectManager.DesignProjects.ItemByName(pName)
    With mP
        Debug.Print "WorkspacePath " & .WorkspacePath
        Debug.Print "ContentCenterPath " & .ContentCenterPath
        Debug.Print "DesignDataPath " & .DesignDataPath
        Debug.Print "FullFileName " & .FullFileName
        'Debug.Print .VaultVirtualPath
        'Debug.Print .ProjectType
        'Debug.Print .TemplatesPath
        'Debug.Print .VaultServer
    End With
End Sub

 

 With this macro you have to map the root project folder within the Vault.

 

As I know, if you use vba you can't access to some Vault API, because these are not COM shared.

The follow link explain it better (https://forums.autodesk.com/t5/inventor-customization/vault-access-through-inventor/td-p/5560858)

 

I'm reading in your post footprint that you are using VS2010, I don't think you did need more explanations on how to proceed.

 

I know this post doesn't solve your problem, but I believe that can be usefull.

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 6 of 20

mr_ensing
Advocate
Advocate

I'll won't be at work before monday, so i can't really test anyting until then. But i think i have to clarify my latest comment: I'm not trying to check in a design by way of VBA. I do however have trouble checking in anything after switching between projects by way of VBA.

 

Method 1:

  1. User starts in project A,
  2. User uses the project dialog to ('manually') activate project B,
  3. User opens a drawing, assy or part (within project B) and tries to check it in the Vault.

Method 2:

  1. User starts in project A,
  2. User applies VBA test code to activate project B,
  3. User opens a drawing, assy or part (within project B) and tries to check it in the Vault.

Test code:

Sub test()
ThisApplication.DesignProjectManager.DesignProjects.ItemByName("I:\...\Project_B.ipj").Activate
End Sub

Method 1 is fine. Method 2 results in the project twilight zone i mentioned before. But I can't find any other way to activate a project (in VBA) than this method. 

 

That being said, thanks for your comment. I'm sure i'm going to be using parts of your code to improve other aspects of my macro.

Thanks.

0 Likes
Message 7 of 20

mr_ensing
Advocate
Advocate

The VS2010 is in my signature because i have it at my disposal. It's used for a addin we run. I'm trying to avoid converting this macro to a VB.NET addin, due to time constraints. But i might try a addin with only the aformentioned test code, to see if this specific issue on solved in VB.NET.

0 Likes
Message 8 of 20

mr_ensing
Advocate
Advocate

Update:

 

It looks like i fixed the errors with the replacing of ReferencedFileDescriptors. It was a simple fix.

 

'I replaced this line:
if stringA = stringB then 'do stuff

'with this line:
if StrComp(stringA, stringB, vbTextCompare) = 0 then 'do stuff

The weird thing is that the problem only occurred in some project-related situations. StringA and StringB would be 100% equal, but the code was not seeing it. The StrComp-function fixes that.

0 Likes
Message 9 of 20

rossano_praderi
Collaborator
Collaborator
Well done!
I'm a little bit confuse, did you posted that part of your code?

I haven't seen it.

Anyway I'm happy to see that you solved this issue by yourself.

Bregs
Rossano Praderi


--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 10 of 20

mr_ensing
Advocate
Advocate

No, that part i did not post before. I mentioned it in the original post:


Two problems arise, both of wich i attribute to the changing of project files in VBA:

  1. The replacing of ReferencedFileDescriptors goes haywire.
    • This only happens if i leave the activating of the copy_object.ipj to the VBA macro itself.
    • When the user changes to copy_object.ipj before using the macro, not a single problem with the ReferencedFileDescriptors.
  2. At the end when the target project-file is activated and the result is opened, it is not possible to check the design in the Vault. Solution is to manually change to a random different project and back.

Problem 2 was easier to isolate, so i focused on that. That issue is still very much alive by the way.

 

Some more info on problem 1, wich i just fixed:

The macro copies a bunch of files to a new path and new filenames. After that the ReferencedFileDescriptors within all these files have to be fixed. That happens in a sub. This sub cycles through the ReferencedFileDescriptors of a file and compares the existing ReferencedFileDescriptors with an old filename. When those are equal, the reference is changed to the new filename.

Somehow this did not work properly when the copy_object project was activated by the macro. Of course, when this (changing project) is done by the user (manually) then there was no problem.

The new method does not care. So that's great. But i still don't understand.

0 Likes
Message 11 of 20

sundaram1087
Advocate
Advocate

Hi

 

             Am facing the same issue while changing the inventor Project file by vb.net . Have you get any solution on that? Could you please share the solution for that if any

 

 

Regards

K.Shunmuga sundaram

 

 

0 Likes
Message 12 of 20

mr_ensing
Advocate
Advocate

No solution from me. I'm still in VBA. Upgraded to IV2018 (from 2015). It's less of a problem now, but it is still there. 

 

I was thinking to rewrite the entire macro and maybe move to VB.NET. I was kind of hoping that VB.NET would fix this problem. I guess not...

0 Likes
Message 13 of 20

sundaram1087
Advocate
Advocate

Hi

 

          Thanks for your reply. 

 

 

Regards

K.Shunmugasundaram

 

 

0 Likes
Message 14 of 20

bulrich-finn
Contributor
Contributor

I have the same issue when I change the project via the COM interface. I can locate the project I want within DesignProjectManager.DesignProjects. I can change the project by calling .Activate on that object. And I can see that the active project apparently changes, both in the Inventor UI and by checking DesignProjectManager.ActiveDesignProject.Name.

 

However, when I go to open files using Apprentice, it pulls references from the wrong location. If I manually change the project in the Inventor UI, then Apprentice behaves nicely.

0 Likes
Message 15 of 20

mr_ensing
Advocate
Advocate

I barely remember this post. But i still have some code changing projects. I'll post my current code.

 

Function ChangeProj(ByVal targetProjectPath As String) As Boolean
    'returns True if succesfull
    Dim CurrentProject As String
    CurrentProject = ThisApplication.DesignProjectManager.ActiveDesignProject.fullfilename
    If StrComp(CurrentProject, targetProjectPath, vbTextCompare) = 0 Then
        ChangeProj = True
        Exit Function
    Else
        ChangeProj = False
    End If
    If ThisApplication.Documents.count > 0 Then
        ThisApplication.Documents.CloseAll
    End If
    If Not FileExists(targetProjectPath) Then
        Exit Function
    End If
    On Error Resume Next
    ThisApplication.DesignProjectManager.DesignProjects.ItemByName(targetProjectPath).Activate
    If Err.Number <> 0 Then
        ThisApplication.DesignProjectManager.DesignProjects.AddExisting (targetProjectPath)
        ThisApplication.DesignProjectManager.DesignProjects.ItemByName(targetProjectPath).Activate
        Err.Clear
    End If
    On Error GoTo 0
    ChangeProj = True
End Function

 

0 Likes
Message 16 of 20

bulrich-finn
Contributor
Contributor

And this code works? Interesting. I'm wondering if this got fixed in a later version of Inventor. We're still currently running 2022.

 

Thanks for sending the code!

0 Likes
Message 17 of 20

mr_ensing
Advocate
Advocate

I re-wrote the entire macro a few years ago. Still in VBA. Works without problems for years by now. Can't remember when or what solved my problems.

 

You're mentioning changing projects in Inventor. But your problem is with Apprentice?

0 Likes
Message 18 of 20

bulrich-finn
Contributor
Contributor

Yes, from what I understand Apprentice follows the active project of Inventor with no way to change that via Apprentice controls.

0 Likes
Message 19 of 20

mr_ensing
Advocate
Advocate

I got curious.

 

I seems you're right. And maybe also wrong.

I tested running Apprentice from Excel VBA. It got the wrong project file. And, in my case, some files from the wrong library location.

 

So i tested some more. I got it to find the right project file and activate it:

Dim invApprentice As New ApprenticeServerComponent

Debug.Print invApprentice.DesignProjectManager.ActiveDesignProject.Name

Dim aProject As DesignProject
For Each aProject In invApprentice.DesignProjectManager.DesignProjects
	If StrComp(aProject.Name, cProject.Full, vbTextCompare) = 0 Then
		Call aProject.Activate
		Exit For
	End If
Next aProject

'if not found
	'Set aProject = invApprentice.DesignProjectManager.DesignProjects.AddExisting("filename")
	'Call aProject.Activate
'end if

Debug.Print invApprentice.DesignProjectManager.ActiveDesignProject.Name

 

0 Likes
Message 20 of 20

bulrich-finn
Contributor
Contributor

Oh, that's great! I had read somewhere that Apprentice doesn't support changing projects, but that's clearly not true. It does apparently pick on on project changes in Inventor, but not consistently. (I think it may have to do with if an instance of Apprentice is already running or not, but I haven't tested to be sure.)

 

Either way, I was able to use your code as an example and my application is now up and running consistently! Thank you for taking the time to help me out!