Hide Table of Contents

Insert New Instance of Virtual Component (C#)

This example shows how to:

  • create an assembly document.
  • insert a new part as a virtual component in the assembly document.
  • insert a new instance of the virtual component in the assembly document.

//-----------------------------------------------------
// Preconditions: None
//
// Postconditions:
// 1. An assembly document is created.
// 2. A virtual part is inserted in the assembly document.
// 3. A new instance of the virtual part is inserted
//    in the assembly document.
// 4. Examine the FeatureManager design tree to
//    verify steps 2 and 3.
// 5. Close the assembly document without saving the modified
//    documents.
//-----------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;

namespace AddNewVirtualPartsCSharp.csproj
{
    
partial class SolidWorksMacro
    {
        
public void Main()
        {
            
string asmTemplate = null;
            asmTemplate = swApp.GetUserPreferenceStringValue((
int)swUserPreferenceStringValue_e.swDefaultTemplateAssembly);

            ModelDoc2 swModel =
default(ModelDoc2);
            swModel = (ModelDoc2)swApp.NewDocument(asmTemplate, 0, 0, 0);

            SelectionMgr swSelMgr =
default(SelectionMgr);
            swSelMgr = (SelectionMgr)swModel.SelectionManager;

            
if (swModel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0) == false)
            {
                
Debug.Print("Failed to select Front plane; check feature name.");
                
return;
            }

            Feature swPlaneFeature =
default(Feature);
            swPlaneFeature = (Feature)swSelMgr.GetSelectedObject6(1, -1);
            RefPlane swPlane =
default(RefPlane);
            swPlane = (RefPlane)swPlaneFeature.GetSpecificFeature2();

            AssemblyDoc swAssem =
default(AssemblyDoc);
            swAssem = (AssemblyDoc)swModel;

            
int lResult = 0;
            Component2 swVirtComp =
default(Component2);
            lResult = swAssem.InsertNewVirtualPart(swPlane,
out swVirtComp);

            
if (lResult == (int)swInsertNewPartErrorCode_e.swInsertNewPartError_NoError)
            {
                Component2 swSecondComp =
default(Component2);
                swSecondComp = (Component2)swAssem.AddComponent5(swVirtComp.GetPathName(), (
int)swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", false, "", 0.1, 0, 0);
            }

        }


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

        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:   Insert New Instance of Virtual Component (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) 2015 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.