Hello everyone,
I'm pretty new with C# macros and i was trying to create a mate faces macro within an assembly, but it just won't build in VS, i keep getting the following error:
'SldWorks' is a 'namespace' but is used like a 'type'
can somebody help please?
i'm using SW 2020 and VS 2015.
here is pasted the SolidWorksMacro.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
namespace m6
{
public partial class SolidWorksMacro
{
public void Main()
{
ModelDoc2 swDoc = null;
PartDoc swPart = null;
DrawingDoc swDrawing = null;
AssemblyDoc swAssembly = null;
bool boolstatus = false;
int longstatus = 0;
int longwarnings = 0;
//
// Open
swDoc = ((ModelDoc2)(swApp.OpenDoc6("E:\\SW api example\\Table.SLDASM", 2, 0, "", ref longstatus, ref longwarnings)));
swAssembly = swDoc;
swApp.ActivateDoc2("Table.SLDASM", false, ref longstatus);
swDoc = ((ModelDoc2)(swApp.ActiveDoc));
swDoc = ((ModelDoc2)(swApp.ActiveDoc));
ModelView myModelView = null;
myModelView = ((ModelView)(swDoc.ActiveView));
myModelView.FrameLeft = 0;
myModelView.FrameTop = 0;
myModelView = ((ModelView)(swDoc.ActiveView));
myModelView.FrameState = ((int)(swWindowState_e.swWindowMaximized));
myModelView = ((ModelView)(swDoc.ActiveView));
myModelView.FrameState = ((int)(swWindowState_e.swWindowMaximized));
boolstatus = swDoc.Extension.SelectByRay(0.016163017996802864, 0.14690640911069863, 0.15107875313559305, -0.98696628942467179, 0.11206479217465459, 0.11549470071889248, 0.00245922486302195, 2, true, 1, 0);
boolstatus = swDoc.Extension.SelectByRay(-0.020442615198021485, 0.056040609040373113, -0.024999999999991473, -0.98696628942467179, 0.11206479217465459, 0.11549470071889248, 0.00245922486302195, 2, true, 1, 0);
swDoc.ClearSelection2(true);
boolstatus = swDoc.Extension.SelectByRay(0.016163017996802864, 0.14690640911069863, 0.15107875313559305, -0.98696628942467179, 0.11206479217465459, 0.11549470071889248, 0.00245922486302195, 2, true, 1, 0);
boolstatus = swDoc.Extension.SelectByRay(-0.020442615198021485, 0.056040609040373113, -0.024999999999991473, -0.98696628942467179, 0.11206479217465459, 0.11549470071889248, 0.00245922486302195, 2, true, 1, 0);
//
// Create CoincidentMateFeatureData
object MateData = null;
swAssembly = ((AssemblyDoc)(swDoc));
MateData = swAssembly.CreateMateData(0);
//
// Set the Entities To Mate
object EntitiesToMate(1) = null;
Set EntitiesToMate(0) = swDoc.ISelectionManager.GetSelectedObject6(1, -1);
EntitiesToMate(1) = ((object)(swDoc.ISelectionManager.GetSelectedObject6(2, -1)));
Variant EntitiesToMateVar;
EntitiesToMateVar = EntitiesToMate;
MateData.EntitiesToMate = EntitiesToMateVar;
//
// Set the Mate Alignment
MateData.MateAlignment = 0;
//
// Create the mate
object MateFeature = null;
swAssembly = ((AssemblyDoc)(swDoc));
MateFeature = swAssembly.CreateMate(MateData);
swDoc.ClearSelection2(true);
swDoc.EditRebuild3();
//
return;
}
// The SldWorks swApp variable is pre-assigned for you.
public SldWorks swApp;
}
}
I'm not sure that's your only problem. I've copied your code into my VS editor and it looks like a mix of VB and C#. There is no 'variant' or 'set' in C#. The whole syntax is wrong.
That being said, I have never created macros in C#, only add-ins. I still use VBA for macros even though I hate it.
If you don't see any other errors, try changing SldWorks to ISldWorks and see if that works. Or change it to SldWorks.SldWorks. The real issue is that SolidWorks uses SldWorks in the namespace (to indicate the location of parts of the code, you use m6) and also as a type of object. So the first one in SldWorks.SldWorks is the namespace, the second is the type.