Replacing Custom Properties

Is there a way to delete the custom properties of a part file and to put new ones back automatically. We have a number of files that do not have the properties we use (e.g. profile  file for the welded mechanic). The goal is to quickly restore the new properties in these files.

We are running SW 2016 Premium version - We do not have the MyCad tools.

 

 

Hello

Look at the task scheduler? I'm not sure but I think it should work.

4 Likes

I just checked and it should work.

http://help.solidworks.com/2016/french/solidworks/sldworks/r_task_sched_task_sidebar.htm

1 Like

Yes, it allows you to update existing properties or create new ones, but there is no possibility to delete existing properties so as not to end up with an important list of properties and unnecessary fields. How can I delete existing properties?

You'll be able to use the Task Scheduler to create and replace your properties, and if you use the macro recorder to delete properties that due doesn't need. You can also launch it with the scheduler and choose the files in question.

Hello

If by macro, here is the function:

Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim config As SldWorks.Configuration
Dim cusPropMgr As SldWorks.CustomPropertyManager
Dim lRetVal As Long
Dim ValOut As String
Dim ResolvedValOut As String
Dim wasResolved As Boolean
Dim nNbrProps As Long
Dim vPropNames As Variant
Dim vPropTypes As Variant
Dim vPropValues As Variant
Dim resolved As Variant
Dim custPropType As Long
Dim K As Long
Dim NomProperty As String

Set cusPropMgr = swModelDocExt.CustomPropertyManager("")

nNbrProps = cusPropMgr.Count
vPropNames = cusPropMgr.GetNames
For K = 0 To nNbrProps - 1
    cusPropMgr.Get2 vPropNames(K), ValOut, ResolvedValOut
    custPropType = cusPropMgr.GetType2(vPropNames(K))
    If vPropNames(K) = "Description" Then
        NomProperty = ResolvedValOut
    End If
    If vPropNames(K) = "Dessinateur" Then
        PropMgr.Delete(vPropNames(K))
    End If
Next K

The lines:

If vPropNames(K) = "Description" Then
        PropertyName = ResolvedValOut
End If

Measure the value of the "Description" custom property.

The lines:

If vPropNames(K) = "Draftsman" Then
        PropMgr.Delete(vPropNames(K))
End If

Delete the "Draftsman" custom property.

You have to be able to juggle that.

Kind regards

Ok, I see the principle but at the level of the macro I don't know enough in VBA, to create it, I tried to save a macro aecla deletion of the properties but it doesn't work ... the macro is almost "empty"


del_pte.swp

Hello

Here is your modified macro with the lines to delete a custom property ("DRAFTER" in this case).

Kind regards


del_pte.swp

Perfect, I want to do one last thing in the task scheduler, I launch the macro with "run the custom task" but can we target this or that folder, I first want to do a test with some of the files.

Hello

When you create your task, you select the folder where your files are, see attached image.

Kind regards


capture.png

Ok, to start the creation of a new property on a folder, but to use the delete macro I use "execute task" (see attached image) there are no fields to point to a particular folder...


2017-09-05_15_20_52-planificateur_de_taches_solidworks.png

I don't know enough about this feature but nothing prevents you from managing it in your macro by loading a file containing the list of your 3D to process. Your macro then has to open each file in SW, delete the custom properties and then save it before moving on to the next one.

Hello

I had thought for a while that the "BatchProperties" utility of MyCad was made for that, but I never managed to do what you want to do....

For I had the same need as you...

If someone uses it and succeeds, it can be useful to acquire it if you have a lot of files to process.

Kind regards

Hubert

Hello

Here's a snippet of C# code that allows you to do what I described in my previous post:

private SldWorks swApp;
        
private void Button2Click(object sender, EventArgs e)
{
if (swApp == null)
{
swApp = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application"));
swApp.Visible = true;
}

string liste = Application.StartupPath + "\\" + "liste.csv";
foreach(string line in System.IO.File.ReadAllLines(liste, System.Text.Encoding.UTF8))
{
string[] parts = line.Split(';');
char[] MyChar = {'"'};
string fichier = parts[0].TrimStart(MyChar).TrimEnd(MyChar);

ModelDoc2 Part = null;
int longstatus = 0;
int longwarnings = 0;

if (fichier.ToLowerInvariant().Contains(".sldasm"))
{
Part = ((ModelDoc2)(swApp.OpenDoc6(fichier, (int)swDocumentTypes_e.swDocASSEMBLY, 0, "", ref longstatus, ref longwarnings)));
}
else if (fichier.ToLowerInvariant().Contains(".sldprt"))
{
Part = ((ModelDoc2)(swApp.OpenDoc6(fichier, (int)swDocumentTypes_e.swDocPART, 0, "", ref longstatus, ref longwarnings)));
}
else
{
continue;
}

//Mettre la fonction de suppression des propriétés personnalisées ici

Part.Save();

swApp.CloseDoc(fichier);
}
}

To be improved and translated into VBA...

Kind regards

Thank you for your feedback and your explanations, but I really lack the skills to make this type of macro ... so I prefer to keep it simple... Is it possible to launch the macro from the task scheduler by specifying a particular folder? In a second time I will add the new properties via the task scheduler (this second part is ok for me)

I don't know if this is possible but in my opinion the macro will still have to analyze all the files present in the folder that would have been passed as an argument by the task scheduler.

For this type of operation to be done, I'm not sure if there is really a need for the task scheduler (it is only used to run the macro at a staggered time).

Kind regards

Ok I understand d. roger but "ac cobra 427" indicates that you can launch the macro from the task  scheduler ...   ac cobra 427  can you tell me the procedure to follow and how to select only the desired files (ok for custom properties, Nok to launch the macro on some files)

Dsl I never needed the task scheduler, I thought it worked because they talk about being able to launch a macro... In the worst case, if you have never had the Mycad tools, you can download and test for 1 month I think...

See screenshots. Run the custom task.


screenshot_2017-09-06-07-01-29.png

Hello

To start a macro by the Task Scheduler, see:

http://help.solidworks.com/2016/french/SolidWorks/sldworks/t_Creating_a_Task_in_Run_Custom_Task_run_custom.htm

To create the macro to launch, see:

http://help.solidworks.com/2016/french/SolidWorks/sldworks/t_Creating_a_SOLIDWORKS_Macro_Template.htm?id=57b26ed5a4094bd386de49ad15179393#Pg0&ProductType=&ProductName=

Kind regards

Otherwise what is the code for VBA to create a property line and associate a value,  I was thinking of doing it with the task planner but it doesn't work very well it doesn't accept empty fields (e.g. the description will be filled in manually afterwards by the draftsman)