Home > System > Deploying your macros and programs

Deploying your macros and programs


There are numerous ways to deploy your various CATIA macros and VBA projects to a larger group of users.  It is possible to create custom toolbars, use macro libraries or even execute them from a file folder.  After experimenting over the years with different methods, I have found that using a catalog works very well.  In this article, I will first discuss some issues associated with using some of those other methods then I’ll discuss the catalog option.

Custom toolbar button

It is possible to easily create your own custom toolbars in CATIA and add commands to them.  You can also define a custom icon for each button and even the tooltip text that should appear when a user hovers their mouse over the icon.   Each button can launch a CATIA command or in this case a macro or VBA project.  I’m not going to discuss how to do this because it is covered well enough in the CATIA help docs (see notes at end of article).

Creating a custom toolbar gives a very professional appearance but unfortunately in most cases, it only works well on your PC.  Distributing and maintaining that toolbar for many users can be a nightmare.  There are actually several reasons for this, but the primary reason is due to the fact that the definition for that custom toolbar and all of its buttons is actually saved in CATSettings files.

In case you aren’t familiar with how CATSettings work I’ll briefly explain.  First, realize that CATSettings aren’t just things you find in the Tools-Options panel – almost everything you do that affects the appearance of CATIA gets saved back to CATSettings.  There are many CATSettings files and they are somewhat divided up by basic categories.  However, one problem is that the same CATSetting file that manages your toolbars also manages many other settings.

The first time you start up CATIA, it usually grabs default settings that your company has defined and copies them to a folder specific to your login.  From that point on, all changes you make are saved in your unique CATSetting files.  When you consider this, what if you need to make a change to one of the macros referenced on a toolbar?  Or remove one?  Or add a new one?  You can make those changes to your company default CATSettings but then to roll them out, you must force everyone to throw away some of their own local setting customizations and take the new default ones.  This may work for just a few buttons or if the macros are very mature and don’t require any maintenance over time but in general it’s not a good option.

Macro library

A macro library is typically a folder containing some CATScript and/or catvbs files or it is a specific catvba project.  Macro libraries can be easily defined from the Tools-Macro dialog panel and this information finally gets saved into the CATSettings.

Overall, this is actually a fairly decent option.  Once the macro libraries are defined, they can quickly be accessed by pressing ALT-F8.  In the case of a folder based macro library, you also get good flexibility to add, modify or delete macros within that folder without impacting users.  However, in the case of VBA based macro libraries, each one is setup as a separate library so like the toolbar option, they are not easily added, removed, etc. for a larger user group.

The user experience with a macro library is ok, but not great especially when the number of programs increases or when you have many different libraries.  It can be cumbersome to switch between a lot of different libraries, there are not visual images for each program and you cannot include additional information about them to help the user understand what each one does.

Deploying macros through a catalog

I like using a catalog to deploy my programs because it offers so much flexibility and provides a good user experience as well.  Like the options discussed above, I am not going to show how to make a catalog.  The CATIA help docs do a decent job of that (see notes at end of article).  Instead, I’ll outline some of the advantages of using catalogs below.

Single organized source

You can very easily provide all your programs through a single catalog.  It doesn’t matter if you have 5 or 500 programs.  You can organize them into as many chapters as you like for easy navigation.  A single catalog can also refer to macros that are stored in any number of file folders across your network.  Additionally, a catalog can point at other catalogs.  This means your company can create one root catalog that points at other separate catalogs for parts, drawings, macros and so on.  Then all users simply access a single catalog that points at these child catalogs.  To the user, it just appears as a single catalog but on the back end, it provides great flexibility for the administrators.  This root catalog should also be defined as a favorite in Tools-Options and moved to the top of the list.  This way, when the user clicks the catalog browser button they always go directly to that “root” catalog.

Metadata (keywords)

The items in a catalog are called components and are stored in families.  Think of families kind of like chapters except that in the catalog world, chapters only create structure while the families hold all the items.  On each family, you have the ability to define keywords.  They may be strings, numbers, etc. and each component in that family can assign its own value to each keyword.  The result is a list of components in the family but each of the columns of keywords are also sortable by clicking on them.  In the case of macros, you might use keywords to define the name of the macro, the version, a description, license requirements and so on.  It might look something like this:

Name Version Description Required License
The best macro 1.0 Does some great stuff None
A better macro 2.3 Does even more stuff KIN
Another macro 1.2 Exports lots of data None

Search

If you have a lot of items in the catalog or maybe have too many chapters, it may not be easy to find what you want.  Luckily, catalogs can be searched (filtered) by the user fairly easily, especially when keywords are added to the families.  The filter will offer a list of all keywords and allow you to define the criteria using “and” logic.  Finally, the results are displayed in a list along with all of the keyword values.

Security

By security, I am referring to the ability to access and execute any given macro or program.  Catalogs only store links to the items they refer to, not the actual documents.  So when it comes to security, you will manage user permissions on the folders that contain those various documents.  The catalog itself does not provide any security.  For example, you might only want only one department to have access to certain macros – just put them in a specific folder and set the permissions accordingly.  If the user selects that item from the catalog and does not have read access to that folder, CATIA will notify them that the file cannot be found or might have been deleted.

Preview Images

A nice aspect of using the toolbar option is the ability to set an icon for each macro.  Catalogs can also provide an image for each macro but they use a larger image than an icon on a toolbar.  This allows you to create an even more detailed preview to help the user recognize a specific function and maybe understand what it does.  For high quality previews, store the images in a file folder that everyone has access to and let the catalog refer to them.

How do I launch a VBA project from a catalog?

Scripts are easy to include in a catalog, but catvba projects require just a bit more work.  I like to launch my VBA projects from a separate script then just let the catalog refer to that script like any other macro.  This can very easily be done using CATIA’s SystemService API.  So, in this scenario, when the user clicks on an item in the catalog, behind the scenes, this is what happens:

User clicks item in catalog → Catalog executes script → Script launches VBA project

You can use essentially the same script as a template for every VBA project and simply edit a few variables each time that define the path of the file, the module name and the procedure name to be run.  Here is an example:

Sub CATMain()

     Dim strPath As String
     Dim strModule As String
     Dim strProcedure As String
     Dim varArgs() As Variant    'Use empty array if procedure has no args
     'Dim varArgs(2) As Variant  'Or size the array if procedure has args
     Dim strMsg As String

     'Define where to find the VBA project and what to run inside it
     strPath = "C:\MyMacros\VBAProject1.catvba"
     strModule = "modMain"
     strProcedure = "CATMain"

     'If the procedure has arguments, define them in the array
     'As an example, a Sub with 3 input arguments in the VBA project
     'Sub CATMain(iInputfile, iOutputFile, iPart)
     'varArgs(0) = "C:\Data\InputData.txt"
     'varArgs(1) = "C:\Data\OutputData.txt"
     'Set varArgs(2) = CATIA.ActiveDocument.Part

     'Launch the VBA project
     On Error Resume Next
     Call CATIA.SystemService.ExecuteScript(strPath, _
                         2, strModule, strProcedure, varArgs)
     If Err.Number <> 0 Then     ‘Any number other than zero is an error
          'Add your own custom error message to the user
          strMsg = "An error occurred while trying to run the macro..."
          MsgBox strMsg, 16, "Error"
     End If

End Sub

Related Help Documentation

For more information on making custom toolbars or creating catalogs, please refer to the CATIA help documentation.  The locations listed below assume you are starting at the help documentation home page.  If you have a question that is not covered in the help docs, post a comment and I’ll try to help.

Creating custom toolbars

Home – Infrastructure – CATIA Infrastructure User Guide – Customizing Toolbars

Creating and managing catalogs

Home – Infrastructure – Component Catalog Editor

Please take a moment to rate this article…Just click the stars up near the title.

Add to: del.icio.us del.icio.us, StumbleUpon StumbleUpon, Digg Digg, Google Google

Advertisements
  1. Fernando
    May 31, 2010 at 11:15 pm

    I agree with everyword you wrote here, very good article.

    I was trying to organize for myself all my macros in a manner that is not depending so much of CATIA and I found a portable software named Slidedock. It was a pretty good solution for Windows (you can organize your macros on chapters – rings in fact, assign different images to them, a.s.o.), but obviously this is not working for UNIX…

    After reading your post in COE forum and here, definitely creating macro catalogs offer a very good solution for both OS, so you can organize scripts very well indeed.

  2. August 11, 2010 at 7:34 pm

    It is hard to find CATIA programming info on the internet. http://forum.zonplm.com may help a little.

  3. November 4, 2010 at 5:02 pm

    Mike,

    At a first glance i already got high expectations of your skills.
    This “Launching a VBA project from a catalog” is my most
    beautiful birthday present in ages!!!

    It’s exactly what i needed. Now our stupid IT department
    can’t mess up my CATIA custom VBA Toolbars anymore…

    I just use & deploy the catalog. it’s great!

    regards, Jan

  4. November 7, 2010 at 4:29 pm

    Hello again,

    Thank you for your comments on ARM catalog.
    Of cource i got busy on collecting my vbaprojects and getting them into a catalog.
    I used the technique as u described in the above article but CATIA does not
    want to save the “CATScript” file.
    See the attached pictures on

    http://www.catia4inventorusers.com/STUFF/Select_CATScript.htm

    I use V5R19 on W7 64 bit machine

    • November 8, 2010 at 10:08 pm

      Sorry Jan – I’m not exactly sure what is causing that problem…I’ve never seen that happen. It sounds almost like something is wrong with your CATIA install. You might try doing the same steps on another computer at your company to see if that is it. Anyway, if you figure it out, please post back what you did in case others encounter that problem.

    • Pavel
      March 24, 2011 at 9:44 am

      Hello Jan,
      I had the same problem as you. Try this to fix your problem:
      1. Normaly Add your macro to catalog
      2.When you receive the error message, just press ok.
      3.Double click on macro item in catalog you have just added. The same window like in 1st step will appear. Now just press ok. You will obtain error message again, but just press ok again.
      4.Press select button in catalog editor and select your macro file again. Now the launch button in catalog editor should become active and you will be able to run your macro.
      For me it works and I run same version of CATIA and Windows as you

  5. November 16, 2010 at 2:21 pm

    Is there any way to take a VBA script (which runs from a UserForm) and have it be wrapped up into something like an .exe? I’ve got a small program written up that I’d like to deploy on several computers in our computer labs, but would like the background programming language to be inaccessible to the end-user which is why I’d like it to be a ‘third-party’ style application. Would I need to take the VBA into something like VB6 to publish it? Thanks!

    • November 16, 2010 at 3:41 pm

      if you can find a version of vb6 kicking around you should be able to import all your modules and forms in then compile it to an exe file. as an alternative if you just want to hide the code from 99 percent of people you could password protect the vba project. that password can be cracked but very few people would likely go that far.

      • January 19, 2011 at 1:40 pm

        Mike, thanks for the help! I finally got a copy of VB6 working (that took FOREVER), but am now having trouble importing my VBA form. I’m guessing that this is due to my references? Think you could go into more detail on how to import that? Thanks again!

      • January 19, 2011 at 10:00 pm

        Can you give me more detail of the problem you are having?

      • January 20, 2011 at 12:00 pm

        Well, I opened VB6 and started a new form identical to the one I created in VBA. From there, I opened the code associated with the form and copy/pasted my code from VBA. After that, I told VB6 which references I needed loaded (CATIA Object Libraries). Now when it’s time to run the form, it hangs up on the line “Set documents1 = CATIA.Documents” in my code. I’m not sure what it’s missing as CATIA is already open and running when the program is initiated.

      • January 20, 2011 at 11:08 pm

        I think the problem is that when you execute code from a separate process from CATIA, you need to get a reference to the CATIA application before you can make calls to it. When you execute scripts or catvba projects, they run inside the CATIA process and there is a global constant called “CATIA” that you can refer to in your code without having to do anything special. So long story short, try something like below. If you still have trouble, let me know exactly what the error says – it’s tough to diagnose without that sort of info.

        Also, from what you said, I assume CATIA is already running…

        Dim CATIA As Object
        On Error Resume Next
        Set CATIA = GetObject(,”CATIA.Application”)
        If Err.Number 0 Then ‘0 means no error
        Msgbox “Please start CATIA!”
        Exit Sub
        End If
        Err.Clear
        On Error Goto 0

      • February 10, 2011 at 2:09 pm

        v5vb :
        if you can find a version of vb6 kicking around you should be able to import all your modules and forms in then compile it to an exe file. as an alternative if you just want to hide the code from 99 percent of people you could password protect the vba project. that password can be cracked but very few people would likely go that far.

        Okay, I’ve gotten my code ported over (with your help!), but now I’m having a really weird error. This same code has worked once before, so this is odd. I’ve got a form where the user inputs values and this script executes them. I’m now getting an ‘Object Required’ error on the line where the input is read. See http://img23.imageshack.us/img23/2271/errorline.jpg for the code. Any ideas? Thanks again, you’ve been awesome so far!

  6. Calin
    November 22, 2010 at 2:09 pm

    Hello Mike!

    Very nice job as usual!

    Please help me to clarify a problem related to the architecture of a VBA project. IMHO one catvba project may contain as many standard modules as I consider it is necessary. The only problem is that all these modules are present in the “Macros” window when pressing ALT-F8. In the happy case when I know which module contains the CATMain function, I can run that module, but another user might get confused and waste precious time doing his/her own research. That is why I think I am not restrained in using just one module; deploying the catvba with a macro catalog solves the problem of identifying where CATMain resides.
    On the other hand, the majority of the catvba projects I have seen so far have one, maybe two standard modules which contain just a few lines of code (usually the CATMain function), the “heavy cavalry”, being positioned into the UserForm.

    Please correct me should you spot flaws in what I have written.

    Thank you very much.

    • November 22, 2010 at 5:10 pm

      One of the reasons i use a script to deploy a vba project is to make it as easy as possible for the user. They shouldn’t have to know what module to run or even what a module is. As for the structure of the vba project, you can have as many modules as you need. I have built projects with up to 20 modules. Last piece of advice is you should put all the guts of your code in modules, not in the forms. The forms are just there to make it possible for the user to interact with the program, but the program should be able to run without any forms. By separating your code this way, you can make drastic UI changes without affectithe guts of the program and what it does.

  7. Pavel
    March 24, 2011 at 9:46 am

    I almost forgot on some important thing.
    Thank you Mike for superb article!!! It helps me a lot.
    Bye

  8. Krings
    May 4, 2011 at 3:06 am

    Hello,

    This article is really well done. I had the same difficulties to deploy macros (that are updated on a regular basis) to some users and this documentation helps me a lot.

    However, I’m facing a problem when the macro that is called is opening a form. The first time (and only the first time) I call this macro, the form is hanging. The buttons are not responding. The next time I run the macro, everything is working fine.

    I found this apar (not solved) at IBM side : http://www-01.ibm.com/support/docview.wss?uid=swg1HD33258

    Did someone get the same behaviour and found a solution ?
    Thanks again.

  9. Thirtha
    May 19, 2011 at 4:08 am

    Hi Krings,

    I am facing the same problem when the vba code has user forms.
    And in my case it is not only for the first time, but no matter how many times I try it does get hang.
    Any suggestions?

    Rgds,
    Thirtha

  10. May 24, 2011 at 10:05 pm

    First, sorry for the slow response… I have been absolutely swamped at home and work for quite some time now.

    First, make sure to launch your form as modeless. I think modal forms will cause a hang condition because the external script calls the sub inside your vba project which displays the form. What happens is CATIA is busy until the original script finishes, but it can’t finish because it is waiting for that form to close. If you launch the form modeless you won’t have this condition.

    Next, I will show below what I have done in my vba projects where I launch a userform from a script. The issue is the form does not fire the Initialize event so you need to check for that and force it.

    1. Inside the userform code module, create a boolean variable that will store whether the form has been initialized or not. Declare this variable outside any procedures in the userform module.

    Private p_blnFormInitialized As Boolean

    2. At the end of the initialization procedure for the form, set this variable to true…so the only way it will be tru is if the initialization happened.

    Private Sub UserForm_Initialize()

    ‘Do all of your initialization statements first

    ‘Finally, set the variable to flag that this
    ‘initialization procedure has executed
    p_blnFormInitialized = True

    End Sub

    3. Create another procedure inside the Userform module to display the form
    and check whether it was initialized.

    Public Sub DisplayForm()

    ‘Show the form – this would usually trigger the Initialize event
    Me.Show vbmodeless

    ‘Now check the value of that variable and if needed, force the initialization
    If p_blnFormInitialized = False then Call UserForm_Initialize

    End Sub

    4. Finally, in a standard module call the DisplayForm procedure. This is the sub you should call from your script.

    Sub CATMain()

    Call frmMain.DisplayForm

    End Sub

  11. benhurME
    March 9, 2012 at 6:22 am

    Does anyone have any expereince with CATvba using Machining workbench , i kinda need help with the AppendOperation and SetGeometry opetions to link a geometry to a curve.

  12. tyc600
    April 10, 2012 at 4:55 am

    v5vb :

    Call frmMain.DisplayForm
    End Sub

    your code is work well ! thanks.
    today,i also encountered this problem.

  13. Vinoth kumar
    April 10, 2012 at 9:23 pm

    Hello Mike,

    I have some problem with macros?
    Please help for me.

    I need to create the macro for changing the properties of dimensions in drafting.

    I create the interactive dimensions in the view.

    macro To do:
    1. Dimension text will change to green color
    2. It will change to basic dimension
    3. Offset value will be 3.(distance between dimension line and text).

    Please take my inputs for your referance.

    Regards,
    Vinoth kumar
    Bangalore

  14. tyc600
    April 11, 2012 at 3:55 am

    on other hand , I have got more simple code ,and work well

    Sub CATMain()
    Load userform1
    userform1.show 0
    End sub

  15. Bonnet charly
    May 24, 2012 at 3:59 am

    Hello,

    This article is really well done.
    my englis is very bad.
    My problem is from :
    – close the catalog at launch the CatScrip.
    – know the path of the CatScrip being
    regards,
    charly

    • May 24, 2012 at 9:18 pm

      I think you mean that you want to automatically close the catalog browser when the script is run from a catalog. If I am right, you cannot do this with a script alone, but you can achieve it using VBA or by launching a standalone exe. It involves making calls to windows APIs in order to get a handle to the catalog browser window and then closing it. I have done this in some of my own projects and will consider this for a future topic. -Mike

  16. charly
    June 14, 2012 at 11:53 am

    Yes exactly my problem
    Charly

  17. cadu1981cadu1981
    July 20, 2012 at 3:38 am

    hI.
    How can i create a macro to rename parts. For example i have one file with name in folder:

    2011_013-001-503_023_01

    and i want to make a similar with other name:

    2011_013-001-503_023_03.

    Normally i make a copy, and then rename the part, open it in CATIA and change name in strcuture again to avoid conflicts. It’s possible to create a macro to change the name of some files in structure to the same in the folder?

    This is good if you are making some units with the similar pars!

  18. September 14, 2012 at 10:43 am

    Hi everyone! very nice resource, thank you!
    Can anyone please tell me how do I export Parameters, Relations with values to an excel sheet (design table) using macros?
    any help will be appreciated. thank you.

  19. Nandhakumar
    January 30, 2013 at 11:16 am

    I encountered a problem while running the macros. what happened is:
    I opened catia and from macros option, i select start recording. In part design, i am drawing a block (cube). then i stop recording and opened that macro, the same block is coming. But, if i tried the same thing for a block with rectangular or circular pattern, it shows error in running that macro. the error shown is “Set hybridShapeLineExplicit1 = hybridShapes1.Item(“X Axis”)”. can you please help me in rectifying that error, it will be useful for my project.
    thank you.

  1. No trackbacks yet.

Leave a Reply to v5vb Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: