A Macro for SW that ''save as. STEP'' from the plan?

Hello Dear community!

Would there be a macro that would allow me to save each part contained in a drawing (up to 30 parts!) in a folder determined and named according to the name of the tab (sheet name) of each part? The task scheduler doesn't work as we need. This would be a very big time saver for us as we have quite complex folding plate assemblies.

If you have any leads, please let me know. The VBA is not my cup but usually I am able to adapt existing ones!

Thank you all for your ideas.

 

Eric

Hello

The idea if I summarize

You have MEP open with several tabs 

You then run a macro that saves on each tab the part defined by default by naming it the name of the tab?

Kind regards

Hello Mad,

yes basically I run the macro which opens each piece, saves in . STEP 214 under the name contained in the part tab in a defined directory (always the same).

Yours truly,

 

Eric

Hello

I sent you a private message.

Have a nice day.

A+

Hello Nicolas,

 

I received the macro but nothing happens. 

It doesn't work on my side.

 

Thank you

 

Eric

Hello

Here is a piece of program that should be able to serve as a starting point, it's C# language so to be translated into VBA:

private SldWorks SWapp;
private string msg = string. Empty;
private string directory = Application.StartupPath + "\\";

private void Button6Click(object sender, EventArgs e)
{
    if (SWapp == null)
    {
        SWapp = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application"));
    }
    
    ModelDoc2 Part;
    Part = ((ModelDoc2)(SWapp.ActiveDoc));
    
    string newFile = Part.IGetDependencies2(true, true,true);
    
    msg = newFile + "\n";
    
    ModelDoc2 swDocTemp = null;
    int longstatus = 0;
    int longwarnings = 0;
    swDocTemp = ((ModelDoc2)(SWapp.OpenDoc6(newFile, (int)swDocumentTypes_e.swDocASSEMBLY, 0, "", ref longstatus, ref longwarnings)));
    
    TraverseAssembly(swDocTemp);
    
    MessageBox.Show(msg);
}

public void TraverseAssembly(ModelDoc2 swDocTemp)
{
    Cursor.Current = System.Windows.Forms.Cursors.AppStarting;

    ConfigurationManager swConfMgr;
    swConf configuration;
    Component2 swRootComp;

    swConfMgr = (ConfigurationManager)swDocTemp.ConfigurationManager;
    swConf = (Configuration)swConfMgr.ActiveConfiguration;
    swRootComp = (Component2)swConf.GetRootComponent3(true);

    TraverseComponent(swRootComp);
}

private void TraverseComponent(Component2 swComp)
{
    object[] vChildComp;
    Component2 swChildComp;
    long i = 0;
    vChildComp = (object[])swComp.GetChildren();
    for (i = 0; i < vChildComp.Length; i++)
    {
        swChildComp = (Component2)vChildComp[i];
        
        TraverseComponent(swChildComp);
        
        if (msg. Contains(swChildComp.GetPathName()) == false)
        {
            msg = msg + swChildComp.GetPathName() + "\n";
            
            ModelDoc2 swDocTemp = default(ModelDoc2);
            ModelDocExtension swModelDocExt = default(ModelDocExtension);
            int longstatus = 0;
            int longwarnings = 0;
            
            swDocTemp = ((ModelDoc2)(SWapp.OpenDoc6(swChildComp.GetPathName(), (int)swDocumentTypes_e.swDocPART, 0, "", ref longstatus, ref longwarnings)));

            string stepFileName = directory + swChildComp.Name2 + ".step";
            swModelDocExt = (ModelDocExtension)swDocTemp.Extension;
            swModelDocExt.SaveAs(stepFileName, 0, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref longstatus, ref longwarnings);
            
            SWapp.CloseDoc(swChildComp.GetPathName());
        }
    }
}

Kind regards

Here is an example of a program using the functions of the previous post.

To use it, you must first open an SLDDRW plan in SolidWorks.

Kind regards


save-step.zip

Hello

 

I still don't have any avenues to explore to solve my problem.

Would there be a macro that would do some of what I'm looking to do that I could supplement as needed?

All I can find is a macro that saves my complete assembly in . Step while I need to have each room save separately under a specific name which can be found in the custom properties or in the MEP tab of the room. 

The most appropriate solution would be for the macro to open the parts of each drawing, saving it in . STEP under the name of a property of the part. The pieces are all contained in a single bet so the macro would pass each MEP.

If you have any leads they are welcome.

 

Kind regards

Eric

Hello

I have bits of macro to compile that must be able to meet the need. If no one gives an answer in between, I think I can provide that tomorrow morning.

Hello

Attached is normally a functional macro (I quickly tested on a two-sheet drawing and didn't see any bugs).

The macro verifies before any action that it is indeed a drawing. I also added a control of the existence of the folder in which the step will be saved.

As it stands, the macro will only export part files, I didn't manage the processing of an assembly in the drawing.

I can complete it if necessary but will require a little more time and if possible a representative file of your drawings to test the behavior (even a drawing with parts, assemblies made up of cubes without any function is enough for me).

As I use microsoft scripts, you will have to activate in vba the microsoft scripting runtime reference.

 


exportstep.txt
3 Likes

Hello

 

Thank you Cyril, I'm going to do some tests on my side with the piece of scrip you gave me.

Thank you so much for your help, really appreciate it!

 

Eric