FYI, SwDMDocument.ReplaceReference doesn't works. Sent a bug report to API Support

Hello everyone,

I've sent a bug report to API Support to show this problem. Code like this doens't works:

SwDMDocument18 dmDoc;

SwDMExternalReferenceOption2 extRefOpt;

...

var res = dmDoc.GetExternalFeatureReferences2(extRefOpt);

String[] externalReferences = (String[])extRefOpt.ExternalReferences;

dmDoc.ReplaceReference(externalReferences[0], );

dmDoc.Save();

dmDoc.CloseDoc();

At the end the file referenced isn't changed.

Regards,

Simone

SolidworksApi macros
Comments 9
JA
JA 2016-04-01

I had problems with this as well (not specifically with External Feature References). In my case, I was just trying to link a drawing to a part. After investigation, I discovered that my macro was not successfully saving the dmDoc after making the change.

dmDoc.Save() has a return value that will reveal whether or not the part saved successfully. You can also try calling GetExternalFeatureReferences2 again after ReplaceReference to see if it was successful.

Edit: I've also read that part and assembly documents need to be opened and rebuilt in order to get the external reference to update.

SP
SP 2016-04-01

Hi John,

As you suggested, I've also tryed to call GetExternalFeatureReferences2 again but the result is the same. Moreover I've used SwDMDocument8.GetChangedReferenced getting an empty list. So I figured that this is a bug.

Simone

SF
SF 2016-04-11

Simone,

Have you heard anything back from API support on this issue?  I am having the same issue when trying to use

dmDoc.AddInvisibleCustomProperty("MyNewProperty", SwDmCustomInfoType.swDmCustomInfoText, "textOfProperty");

dmDoc.Save();

SP
SP 2016-04-12

Hi Richard,

I've recently received an API support answer. Essentialy the issue is about API help misleading.

In the remarks section of ISwSMDocument::ReplaceReference functions it's written: "Before calling this method, you must call ISwDMDocument::GetAllExtenalReferences4" and effectively I didn't. But, why ? Going to remarks section of ISwDMDocument::GetAllExtenalReferences4 function it's written: " Use this method for drawings; use ISwDMDocument15::GetExternalFeatureReferences for parts and assemblies" and because I was working on assembly, I did it. So replacing ISwDMDocument15::GetExternalFeatureReferences by ISwDMDocument::GetAllExtenalReferences4, all works well.

I didn't find AddInvisibleCustomProperty function. Maybe, did you mean ISwDMDocument::AddCustomProperty instead ?

Regards,

Simone

SF
SF 2016-04-12

Glad to hear you got your issue resolved.  I may need to do something similar with the custom properties.  AddInvisibleCustomProperty is part of document manager api:

SwDMDocument15::GetInvisibleCustomProperty

SwDMDocument15::AddInvisibleCustomProperty

SwDMDocument15::SetInvisibleCustomProperty

By default, all solidworks files contain 44 of these properties (ex. Author, Created Date, ...).  These are properties that can not be changed by the user but can be changed through the api.

SF
SF 2016-04-12

I finally figured out my issue.  It was the same issue that you were having.  I needed to get the document before trying to add to it.  Below is the code I used.  This is very useful for adding properties to files that the user will not be able to change.  I am using it to add a unique identifier to the file to help in managing our library of files.

const string sLicenseKey = "Insert your number";

const string docPath = "C:\\NEW_slotted capstan screw_din.sldprt";

const string SWID = "Insert your Unique Identifier";

SwDMClassFactory swClassFact = default(SwDMClassFactory);

SwDMApplication swDocMgr = default(SwDMApplication);

SwDMDocument3 swDoc = default(SwDMDocument3);

SwDmDocumentType nDocType = 0;

SwDmDocumentOpenError nRetVal = 0;

swClassFact = new SwDMClassFactory();

swDocMgr = swClassFact.GetApplication(sLicenseKey);

swDoc = (SwDMDocument3)swDocMgr.GetDocument(docPath, nDocType, false, out nRetVal);

propCount = swDoc.GetInvisibleCustomPropertyCount();

propNames = swDoc.GetInvisibleCustomPropertyNames();

swDoc.AddInvisibleCustomProperty("SWID", SwDmCustomInfoType.swDmCustomInfoText, SWID);

swDoc.Save();

swDoc.CloseDoc();

SP
SP 2016-04-13

Hi Richard,

great news from you. About invisible custom properties, where did you get this information ? I've searched into API help but I didn't find anything about it. Have you a "backdoor" into Solidworks Corps ? ;-)

Regards,

Simone

SF
SF 2016-04-13

No backdoor but I found the information in the document manager api under ISwDMDocument15.  Here is a direct link:

2016 SOLIDWORKS API Help - ISwDMSheet Interface Members

SP
SP 2016-04-13

Ok I just found it. The interface type is SwDMSheet not SwDMDocument. Anyway I've tested your code and it works well, so the methods you have mentioned are on the both interfaces. A doubt remain: I've found nothing about AddInvisibleCustomProperty function...