SolidWorks Express
API TIP: How to Marshal an Object to a Variant in .NET
 
Some SolidWorks methods have input object parameters that must be marshaled to Variants because System.Object has replaced Variant in the .NET framework.

To verify that you must marshal an input object parameter to a Variant, examine the method's VBA syntax by clicking the link located in the Visual Basic 6 Syntax section in the method's topic in the SolidWorks API Help. If the data type of the input object parameter is Variant, then you must marshal the input object parameter to a Variant; otherwise, your code might generate an error or not work as expected.

The following examples demonstrate how to pass an array of bodies to IAssemblyDoc::ReorderComponents in VB.NET and C# by marshaling an input object parameter to a Variant. You use wrapper class System.Runtime.InteropServices.DispatchWrapper to marshal this object.

VB.NET example:

'-------------------------------------------------------

'

' This example shows how to move selected assembly components to a

' newly created folder in the FeatureManager design tree. 

'

' Preconditions: Specified assembly document to open exists.

'

' Postconditions:

' 1. Assembly document is opened in .

' 2. The valve<1> and valve_guide<1> components are selected.

' 3. Folder named Folder1 is created in the FeatureManager design tree.

' 4. The valve<1> and valve_guide<1> components are moved to Folder1,

'    which you can verify by expanding the Folder1 folder.

'

' NOTE: Because the specified assembly document is used by

' an online SolidWorks tutorial, do not save any changes when

' closing the document.

'

'--------------------------------------------------------

Imports SolidWorks.Interop.sldworks

Imports SolidWorks.Interop.swconst

Imports System

Imports System.Runtime.InteropServices

Partial Public Class SolidWorksMacro

Public Sub Main()

Dim errors As Integer = 0

Dim warnings As Integer = 0

Dim status As Boolean = False

 

'Open assembly document

swApp.OpenDoc6("C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\motionstudies\valve_cam.sldasm", CInt(swDocumentTypes_e.swDocASSEMBLY), CInt(swOpenDocOptions_e.swOpenDocOptions_Silent), "", errors, warnings)

Dim modelDoc2 As ModelDoc2 = DirectCast(swApp.ActiveDoc, ModelDoc2)

Dim assemblyDoc As AssemblyDoc = DirectCast(modelDoc2, AssemblyDoc)

Dim featureMgr As FeatureManager = DirectCast(modelDoc2.FeatureManager, FeatureManager)

 

'Select and get the two valve-related components to move to the new folder

Dim modelDocExt As ModelDocExtension = modelDoc2.Extension

Dim selectionMgr As SelectionMgr = DirectCast(modelDoc2.SelectionManager, SelectionMgr)

status = modelDocExt.SelectByID2("valve-1@valve_cam", "COMPONENT", 0, 0, 0, True, 0,Nothing, 0)

Dim selObj As Object = selectionMgr.GetSelectedObject6(1, -1)

status = modelDocExt.SelectByID2("valve_guide-1@valve_cam", "COMPONENT", 0, 0, 0, True,0,Nothing, 0)

selObj = selectionMgr.GetSelectedObject6(2, -1)

Dim count As Integer = selectionMgr.GetSelectedObjectCount2(0)

Dim componentsToMove As Object() = New Object(count - 1) {}

For i As Integer = 0 To count - 1

componentsToMove(i) = selectionMgr.GetSelectedObjectsComponent3(i + 1, 0)

Next

 

'Create the folder where to move the selected components

Dim feature As Feature = featureMgr.InsertFeatureTreeFolder2(CInt(swFeatureTreeFolderType_e.swFeatureTreeFolder_EmptyBefore))

feature = DirectCast(assemblyDoc.FeatureByName("Folder1"), Feature)

 

'Marshal .NET object to Variant using DispatchWrapper

compsToMove = ObjectArrayToDispatchWrapperArray(componentsToMove)

modelDoc2.ClearSelection2(True)

 

'Move the selected components to the new folder

retVal = assemblyDoc.ReorderComponents(compsToMove, feature, CInt(swReorderComponentsWhere_e.swReorderComponents_LastInFolder))

End Sub

'

Public Function ObjectArrayToDispatchWrapperArray(ByVal SwObjects As Object()) As DispatchWrapper()

Dim arraySize As Integer

arraySize = SwObjects.GetUpperBound(0)

 

Dim dispwrap As DispatchWrapper() = New DispatchWrapper(arraySize) {}

Dim arrayIndex As Integer

 

For arrayIndex = 0 To arraySize

dispwrap(arrayIndex) = New DispatchWrapper(SwObjects(arrayIndex))

Next

Return dispwrap

End Function

 

''' <summary>

''' The SldWorks swApp variable is pre-assigned for you.

''' </summary>

Public swApp As SldWorks

Public retVal As Boolean

Public compsToMove As DispatchWrapper()

 

End Class

C# example:

//-------------------------------------------------------

// This example shows how to move selected assembly components to a

// newly created folder in the FeatureManager design tree.

//

// Preconditions: Specified assembly document to open exists.

//

// Postconditions:

// 1. Assembly document is opened.

// 2. The valve<1> and valve_guide<1> components are selected.

// 3. Folder named Folder1 is created in the FeatureManager design tree.

// 4. The valve<1> and valve_guide<1> components are moved to Folder1,

//    which you can verify by expanding the Folder1 folder.

//

// NOTE: Because the assembly document is used by an online

// SolidWorks tutorial, do not save any changes when

// closing the document.

//

//--------------------------------------------------------

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System;

using System.Runtime.InteropServices;

 

namespace ReOrderComponentsMacro.csproj

{

public partial class SolidWorksMacro

{

public void Main()

{

int errors = 0;

int warnings = 0;

bool status = false;

 

//Open assembly document

swApp.OpenDoc6("C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\motionstudies\\valve_cam.sldasm", (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

ModelDoc2 modelDoc2 = (ModelDoc2)swApp.ActiveDoc;

 

AssemblyDoc assemblyDoc = (AssemblyDoc)modelDoc2;

FeatureManager featureMgr = (FeatureManager)modelDoc2.FeatureManager;

 

//Select and get the two valve-related components to move to the new folder

ModelDocExtension modelDocExt = modelDoc2.Extension;

SelectionMgr selectionMgr = (SelectionMgr)modelDoc2.SelectionManager;

status = modelDocExt.SelectByID2("valve-1@valve_cam", "COMPONENT", 0, 0, 0, true, 0, null, 0);

object selObj = selectionMgr.GetSelectedObject6(1, -1);

status = modelDocExt.SelectByID2("valve_guide-1@valve_cam", "COMPONENT", 0, 0, 0, true, 0, null, 0);

selObj = selectionMgr.GetSelectedObject6(2, -1);

int count = selectionMgr.GetSelectedObjectCount2(0);

object[] componentsToMove = new object[count];

for (int i = 0; i < count; i++)

{

componentsToMove[i] = selectionMgr.GetSelectedObjectsComponent3(i + 1, 0);

}

//Create the folder where to move the selected components

Feature feature = featureMgr.InsertFeatureTreeFolder2((int)swFeatureTreeFolderType_e.swFeatureTreeFolder_EmptyBefore);

feature = (Feature)assemblyDoc.FeatureByName("Folder1");

 

//Marshal .NET object to Variant by using DispatchWrapper

compsToMove = ObjectArrayToDispatchWrapperArray(componentsToMove);

modelDoc2.ClearSelection2(true);

 

//Move the selected components to the new folder

retVal = assemblyDoc.ReorderComponents(compsToMove, feature, (int)swReorderComponentsWhere_e.swReorderComponents_LastInFolder);

}

 

public DispatchWrapper[] ObjectArrayToDispatchWrapperArray(object[] SwObjects)

{

int arraySize;

arraySize = SwObjects.GetUpperBound(0);

DispatchWrapper[] dispwrap = new DispatchWrapper[arraySize + 1];

int arrayIndex;

 

for (arrayIndex = 0; arrayIndex < arraySize + 1; arrayIndex++)

{

dispwrap[arrayIndex] = new DispatchWrapper(SwObjects[arrayIndex]);

}

return dispwrap;

}

/// <summary>

/// The SldWorks swApp variable is pre-assigned for you.

/// </summary>

public SldWorks swApp;

public bool retVal;

public DispatchWrapper[] compsToMove;

}

}

 

 

 

For more information about marshaling objects using wrapper classes, see:

http://msdn.microsoft.com/en-us/library/2x07fbw8(VS.71).aspx#cpcondefaultmarshalingforobjectsanchor3

 
 
   
 
 
>> SIGN UP for SolidWorks Express Newsletter and get the tips and tricks delivered into your inbox every two weeks!