Solidworks API Add Part Property and then see it in the drawing

Hello

I have a macro that adds a "Nom_Modificateur" property in a part, The problem is that when I open my layout (still in the macro) the background does not change, I am forced to edit the background plan then explode the block and the my given appears. To know if from the beginning I burst the block, the macro works perfectly. But I don't want to break the block because I have another macro that also writes cleanliness that is in the cartridge and that works while the code is identical.

Here's the code:

Set Part = swApp.OpenDoc6(File, 1, 0, "", longstatus, longwarnings)
    Set swApp = Application.SldWorks
    'We get the active document
    Set swModel = swApp.ActiveDoc

swCustPropMgr.Add3 "Type_Validation", swCustomInfoText, "", 2
swCustPropMgr.Add3 "Date_Validation", swCustomInfoText, "", 2
swCustPropMgr.Add3 "Index", swCustomInfoText string, 2
If string = "02" Then
swCustPropMgr.Add3 "Raison_Modification_A02", swCustomInfoText, Reason, 2
swCustPropMgr.Add3 "Nom_Modificateur_A02", swCustomInfoText, Nom_Modif, 2
ElseIf string = "03" Then
swCustPropMgr.Add3 "Raison_Modification_A03", swCustomInfoText, Reason, 2
swCustPropMgr.Add3 "Nom_Modificateur_A03", swCustomInfoText, Nom_Modif, 2
ElseIf string = "04" Then
swCustPropMgr.Add3 "Raison_Modification_A04", swCustomInfoText, Reason, 2
swCustPropMgr.Add3 "Nom_Modificateur_A04", swCustomInfoText, Nom_Modif, 2
End If

swApp.CloseDoc FileName

Thank you 

Hello

 

Complicated to work with so little give, can you put the code of your other macro? When you launch the other one you are already on the drawing, or does it also open a document?

Hello

Just edit the background and then explode your block and re-save the basemap template as a block and by default locked in editing.

2 Likes

Thank you for your answer,

I'll put the full macro on you, I'll run it from an assembly-->

- You have to select a component from the component tree and then launch the macro

(I know that the macro is not optimized but before optimizing it I try to make everything work)

 


3_prise_dindice.swp

I have several .slddrt plan formats(A4H, A4V, A3H...) All of these formats have a block link to a block file to define everything that should be in the title block (.sldblk file). Thanks to this, if I want to add an element I add it to the sldblk file and all my formats are modified. It's better than editing on all plan formats (I'm 30)

So if my basemap has an exploded block, will I lose the link?

Yes, you lose the link because it will be shattered.

Hello

Do you really have to break up the block for the changes to be taken into account or is a simple reconstruction enough?

For your information, the changes made by macro quite often require a reconstruction to be taken into account, it's the famous CTRL + Q which is a bit of a reflex.

Kind regards

1 Like

d.roger, Reconstruction does nothing

The macro cannot work with the block because the block is write-protected unless you add a box directly to the modifier name and populate in a property of the part.

That's exactly what I do, I add a personal property to my 3d file and in my cartridge I have the name of this property which are the same. But my problem is that it doesn't show up. And even if I explode my block beforehand, I still have to edit the background plan for it to appear

 

And how do the other properties appear in the title block?

In the same way and there is no problem(See attachment). That's why I don't understand at all


propriete_piececartouche.png

But that'  s where you have to add the "Nom_Modificateur" property and then create the link in the block.

1 Like

Yes that's what I do, when I look at my part properties, they are indeed added, but nothing in the title block while there is a link

But there in your properties there is no "Nom_Modificateur" according to the screen print you made...

As ac cobra says, the variable doesn't seem to exist so your code would have to match this:

swCustPropMgr.Add3 "Type_Validation", swCustomInfoText, "", 0
swCustPropMgr.Add3 "Date_Validation", swCustomInfoText, "", 0
swCustPropMgr.Add3 "Index", swCustomInfoText string, 0
If string = "02" Then

...

 

On the link below you see that the variables must exist if you put a "2" at the end, it replaces the value only if the latter exists. With a 0 it creates the variable if it is non-existent.

http://help.solidworks.com/2018/english/api/swconst/SOLIDWORKS.Interop.swconst~SOLIDWORKS.Interop.swconst.swCustomPropertyAddOption_e.html

 

On the other hand, be careful you have to plan an increment when going, otherwise the first time you use it will create the A02 - A03 .... and the next time you use them, since they are already created, he won't do anything. and if you put "1" it will rewrite on it each time so you won't have a history anymore.

I solved my problem. During the first macro that inserts data I put all my future index values and I don't know why, when it has already created a value, the second time it does the update by itself. 

Thank you for all your answers, I know that not everything is clear...