Error when using C# to access to SolidWorks

Hi Everyone, I am a newbie to C#

I follow XCad instructions to try to open a customize command manager tab in SolidWorks.
However, it keeps popping up an error:
“The expression .Substring(0, 4) cannot be evaluated. Index and length must refer to a location within the string. Parameter name: length”

As no line and code for this error, I have totally no idea how to solve it.
Could anyone help?

(Implementing SOLIDWORKS® Top Ten List Ideas 2020 from scratch using xCAD.NET - YouTube)

1 Like

@artem : This is your lib! :smiley:

@Ryan, do you have this during the compilation, i.e. in the Visual Studio or at runtime? I can see Substring(0, X) is used once in the code to extract the version of the file which I believe is not the case here.

And it is also used in the project targets to extract the current version of the framework. And I believe later one is the case. If so, please make sure you are using either .NET Framework or .NET Core , .NET 5 is not supported yet as per this ticket. My recommendation to use .NET Framework instead of .NET Core for SOLIDWORKS add-ins.

@artem
I am using Visual Studio to code (Community 2015)
using .NET Framework 4.6.1 currently,
and these are the Nuget package I have downloaded and used as Reference

this time I am using a simpler code captured on Xcad platform,
but seems the same problem happen…

Ah, I see. Visual Studio 2015 is quite old and I believe has a different structure (could you please attach this .csproj file here so I can inspect it) - I am using 2019 Community. I think you can make it work in a slightly different way:

  • Unload the project and edit content (alternatively just open your *.csproj file in the notepad)
  • Remove the following portion from the project:

  • Now you need to add this into your postbuild events

“%Windir%\Microsoft.NET\Framework64\v4.0.30319\regasm” /codebase “$(TargetPath)”

image

Thanks @artem
Another error message pop up after following above instruction

Here are the files:
https://drive.google.com/drive/folders/1zcZHID51JiL0bys7MdzPnASDdHi6D22d?usp=sharing

I will check tomorrow but try running your visual studio as admin

Hi Ryan,

Is there a specific reason why you have to use VS 2015?

If not I would recommend to follow the flow and use VS2019, which is actively used for development/testing.

Eddy

2 Likes

I try using 2019 at the beginning but not even work in recording VSTA macro in SolidWorks…
would reinstall SW and VS 2019 and try again tonight.

@artem, not working, would try it again on VS 2019,
also I am using SolidWorks 2019 SP5 currently, don’t know whether this will affect or not, fyi

Hi Ryan,

I try using 2019 at the beginning but not even work in recording VSTA macro in SolidWorks…

I tested the VSTA macro recording after a long time of not using it and it opened up in VS2015 automatically.
Solidworks 2021 + VS2015 + VS2019 installed
I also had installed Microsoft Visual Studio 2005 Tools for Applications which was opening the macro in older times (VSTA 2.0 only if I remember correctly). VS2015 took over apparently.

Solidworks 2020 does the same: opening the macro in VS2015. But I had to switch on the option:
Enable VSTA Version 3.0.
Maybe you should do that also in 2019 ?

Eddy

Hi @Eddy , here is the case and that’s why I am using VS 2015:
what I just done is uninstall VS 2015 and SolidWorks
then have a clean installation for VS 2019 and the VSTA Tools
then install SolidWorks 2019 again
and click the Enable VSTA Version 3.0.

and…not even the VSTA macro option pops up

for this question I have already tested different methods and finally find a forum reply that work, but requiring me to use VS2015:
3DEXPERIENCE Platform (3ds.com)

Sorry to hear the approach that works for 2020 doesn’t work for 2019