IModelDocExtension::InsertAttachment Method Crashes SW


I am writing a text file in my temp directory and then inserting it into the design binder.

It works perfect.

When I want to regenerate the file I thought to delete the attachment and then reinsert the file.

Unfortunately Solidworks crashes when I do this.

    swModel.Extension.DeleteAttachment FileSystem.GetFileName(tempFilePath)
    swModel.Extension.InsertAttachment tempFilePath, False

If I comment out either of the above lines the macro runs fine, only when they both run together there is a problem.

On a hunch I tried adding a Sleep interval in between the calls (also a rebuild) and it did work a couple of times, but then crashed again.

Any ideas?



So it was late last night, but this morning I went to the trouble of actually getting the attachments and looping through them.

I noticed that internally Solidworks stores the complete path of the file. In my head I presumed as an unlinked file is embedded I would only need the filename.

Passing in the full path seems to have fixed the issue.

    swExtension.DeleteAttachment tempFilePath
    swExtension.InsertAttachment tempFilePath, False

Just shows the danger of making assumptions / working late / being lazy… I swear it worked some of the time lol

Hey Rob You know I don’t understand macros, I was just wondering,
Why don’t you create them directly in the design file? (it can’t be created?)
And won’t it overwrite the old file when you want to create it again?

Hey Omur
I am still experimenting with what will work best.
I actually want to overwrite the file as it is much easier to make new than trying to modify an existing file

When you attach a file you have the option to link it or embed it.

I was hoping to embed them but have since discovered that there is no way to open them again if they are embedded.

Well you can open them from the ui - but what is actually happening is solidworks copies the file to a temp directory and opens up a copy.

With this new knowledge I am forced to link them… which makes this question moot because I could simply overwrite the existing file and solidworks would be non the wiser :rofl:

every days a school day

1 Like