Hide Table of Contents

Copy Components With Mates to Assembly Example (C#)

This example shows how to copy components with mates in an assembly.

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

// Preconditions:

// 1. Open:

// <SolidWorks_install_dir>\samples\tutorial\driveworksxpress\mobile gantry.SLDASM.

// 2. Observe Leg<1> and Leg<2> in the FeatureManager design tree.

//

// Postconditions:

//    Leg<1> is replaced by a copy of Leg<2>:  observe Leg<3> in the mobile gantry.

//

// NOTE: Because this assembly is used in a SolidWorks online tutorial,

//       do not save any changes when you close it.

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

using SolidWorks.Interop.sldworks;

using System.Runtime.InteropServices;

namespace CopyComponentsWithMatesToAssembly_CSharp.csproj

{

    partial class SolidWorksMacro

    {

        public void Main()

        {

            ModelDoc2 swModel = default(ModelDoc2);

            ModelDocExtension swModelDocExt = default(ModelDocExtension);

            SelectionMgr swSelMgr = default(SelectionMgr);

            bool[] blRepeat = new bool[3];

            bool[] blFlip = new bool[3];

            object SelectedComps = null;

            object RepeatOptions = null;

            object[] MateRefs = new object[3];

            object InpValues = null;

            object FlipValues = null;

            AssemblyDoc swAssy = default(AssemblyDoc);

            Component2 swItem = default(Component2);

            Feature swFeature = default(Feature);

            RefPlane refPlane = default(RefPlane);

            object[] arrMateEntities = new object[3];

            object[] arrCompsToCopy = new object[1];

            bool boolstatus = false;

            double[] dValues = new double[3];

            // Disable Visual Basic error on implicit Query Interface

            //On Error Resume Next

            swModel = (ModelDoc2)swApp.ActiveDoc;

            swAssy = (AssemblyDoc)swModel;

            swSelMgr = (SelectionMgr)swModel.SelectionManager;

            swModelDocExt = swModel.Extension;

            boolstatus = swModelDocExt.SelectByID2("Leg-1@mobile gantry", "COMPONENT", 0, 0, 0, false, 0, null, 0);

            swModel.EditDelete();

            swModel.ClearSelection2(true);

            boolstatus = swModelDocExt.SelectByID2("Leg-2@mobile gantry", "COMPONENT", 0, 0, 0, false, 0, null, 0);

            swItem = (Component2)swSelMgr.GetSelectedObject6(1, -1);

            arrCompsToCopy[0] = swItem;

            arrMateEntities[0] = null;

            arrMateEntities[1] = null;

            swModel.ClearSelection2(true);

            boolstatus = swModelDocExt.SelectByID2("Left End@Universal Beam-1@mobile gantry", "PLANE", 0, 0, 0, false, 0, null, 0);

            swFeature = (Feature)swSelMgr.GetSelectedObject6(1, -1);

            refPlane = (RefPlane)swFeature.GetSpecificFeature2();

            arrMateEntities[2] = refPlane;

            swModel.ClearSelection2(true);

            blRepeat[0] = true;

            blRepeat[1] = true;

            blRepeat[2] = false;

            SelectedComps = arrCompsToCopy;

            RepeatOptions = blRepeat;

            MateRefs = arrMateEntities;

            dValues[0] = 0.0;

            dValues[1] = 0.0;

            dValues[2] = 0.0;

            InpValues = dValues;

            blFlip[0] = false;

            blFlip[1] = false;

            blFlip[2] = false;

            FlipValues = blFlip;

            DispatchWrapper[] disparrCompsToCopy = new DispatchWrapper[1];

            disparrCompsToCopy[0] = new DispatchWrapper(arrCompsToCopy[0]);

            DispatchWrapper[] dispMateRefs = new DispatchWrapper[3];

            dispMateRefs[0] = new DispatchWrapper(MateRefs[0]);

            dispMateRefs[1] = new DispatchWrapper(MateRefs[1]);

            dispMateRefs[2] = new DispatchWrapper(MateRefs[2]);

            //Repeat all mates except the coincident mate with Right End@Universal Beam-1

            swAssy.CopyWithMates((disparrCompsToCopy), RepeatOptions, (dispMateRefs), InpValues, FlipValues);

            swAssy.EditRebuild();

        }

        public SldWorks swApp;

    }

}



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Copy Components With Mates to Assembly Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2014 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.