Hide Table of Contents

Add Pin Connector Example (C#)

This example shows how to add a pin connector to a static study.

//----------------------------------------------------------------------------
// Preconditions:
// 1. Add the SOLIDWORKS Simulation as an add-in
//    (in SOLIDWORKS, click Tools > Add-ins > SOLIDWORKS Simulation).
// 2. Add the SOLIDWORKS Simulation primary interop assembly as a reference 
//    (in the IDE's Project Explorer, right-click
//     the project name, click Add Reference, click the Browse tab,
//     navigate to install_dir\api\redist\CLR2
//     click SolidWorks.Interop.cosworks.dll, and click OK).
// 3. Ensure that the specified model document exists.
// 4. Open an Immediate window.
//
// Postconditions:
// 1. Opens the model.
// 2. Activates the Ready study.
// 3. Deletes the pin between branches connector.
// 4. Adds Pin Connector-3 to the Connectors folder in the Simulation study
//    tree.
// 5. Prints the properties of Pin Connector-3 to the Immediate window.
// 6. Meshes the model.
// 7. Analyzes Ready.
// 8. Inspect the Immediate window, the Simulation Study tree, and the
//    graphics area.
//
// NOTE: Because the model is used elsewhere, do not save changes.
// ---------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.cosworks;
using System.Runtime.InteropServices;
namespace AddPinConnector_CSharp.csproj
{
    partial class SolidWorksMacro
    {
 
        ModelDoc2 Part;
        CosmosWorks COSMOSWORKS;
        CwAddincallback COSMOSObject;
        CWModelDoc ActDoc;
        CWStudyManager StudyMngr;
        CWStudy Study;
        CWLoadsAndRestraintsManager lrMngr;
        CWPinConnector pinConnector;
        CWMesh CwMesh;
        string str3;
        string str4;
        object[] varArray1 = new object[1];
        object[] varArray2 = new object[1];
        object pDisp3;
        object pDisp4;
        byte[] var20;
        byte[] var21;
        int errCode;
        double el;
        double tl;
        int longstatus;
        int longwarnings;
 
 
        public void Main()
        {
            Part = swApp.OpenDoc6("C:\\Program Files\\SOLIDWORKS Corp\\SOLIDWORKS\\Simulation\\Examples\\pliers.sldasm", 2, 0, ""ref longstatus, ref longwarnings);
            Part = (ModelDoc2)swApp.ActiveDoc;
 
            COSMOSObject = (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");
            if (COSMOSObject == null)
                ErrorMsg(swApp, "Failed to get Simulation add-in"true);
 
            COSMOSWORKS = COSMOSObject.CosmosWorks;
            if (COSMOSWORKS == null)
                ErrorMsg(swApp, "Failed to get COSMOSWORKS"true);
 
            ActDoc = COSMOSWORKS.ActiveDoc;
            if (ActDoc == null)
                ErrorMsg(swApp, "Failed to get active document"true);
 
            StudyMngr = ActDoc.StudyManager;
            if (StudyMngr == null)
                ErrorMsg(swApp, "Failed to get study manager"true);
 
            Study = StudyMngr.GetStudy(0);
            if (Study == null)
                ErrorMsg(swApp, "Failed to get frequency study"true);
 
            StudyMngr.ActiveStudy = 0;
 
            str3 = "64,31,0,0,3,0,0,0,255,254,255,22,115,0,101,0,99,0,111,0,110,0,100,0,32,0,98,0,114,0,97,0,110,0,99,0,104,0,45,0,49,0,64,0,112,0,108,0,105,0,101,0,114,0,115,0,4,0,0,0,16,0,0,0,1,0,0,0,1,0,0,0,12,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,5,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,84,67,0,58,0,92,0,80,0,114,0,111,0,103,0,114,0,97,0,109,0,32,0,70,0,105,0,108,0,101,0,115,0,92,0,83,0,79,0,76,0,73,0,68,0,87,0,79,0,82,0,75,0,83,0,32,0,67,0,111,0,114,0,112,0,92,0,83,0,79,0,76,0,73,0,68,0,87,0,79,0,82,0,75,0,83,0,92,0,83,0,105,0,109,0,117,0,108,0,97,0,116,0,105,0,111,0,110,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,10";
            str3 = str3 + "1,0,115,0,92,0,115,0,101,0,99,0,111,0,110,0,100,0,32,0,98,0,114,0,97,0,110,0,99,0,104,0,46,0,115,0,108,0,100,0,112,0,114,0,116,0,9,128,255,254,255,13,115,0,101,0,99,0,111,0,110,0,100,0,32,0,98,0,114,0,97,0,110,0,99,0,104,0,2,0,0,164,247,240,62,0,33,23,28,65,1,0,0,0,0,0,0,0,4,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,33,23,28,65,29,0,0,0,70,252,240,62,34,0,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,29,0,0,0,70,252,240,62,0,0,0,0,1,0,0,0,0,0,255,255,1,0,24,0,109,111,69,110,100,70,97,99,101,51,73,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,39,0,0,0,85,255,240,62,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
 
            str4 = "64,31,0,0,3,0,0,0,255,254,255,21,102,0,105,0,114,0,115,0,116,0,32,0,98,0,114,0,97,0,110,0,99,0,104,0,45,0,49,0,64,0,112,0,108,0,105,0,101,0,114,0,115,0,4,0,0,0,16,0,0,0,1,0,0,0,1,0,0,0,11,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,5,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,83,67,0,58,0,92,0,80,0,114,0,111,0,103,0,114,0,97,0,109,0,32,0,70,0,105,0,108,0,101,0,115,0,92,0,83,0,79,0,76,0,73,0,68,0,87,0,79,0,82,0,75,0,83,0,32,0,67,0,111,0,114,0,112,0,92,0,83,0,79,0,76,0,73,0,68,0,87,0,79,0,82,0,75,0,83,0,92,0,83,0,105,0,109,0,117,0,108,0,97,0,116,0,105,0,111,0,110,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0";
            str4 = str4 + ",115,0,92,0,102,0,105,0,114,0,115,0,116,0,32,0,98,0,114,0,97,0,110,0,99,0,104,0,46,0,115,0,108,0,100,0,112,0,114,0,116,0,9,128,255,254,255,12,102,0,105,0,114,0,115,0,116,0,32,0,98,0,114,0,97,0,110,0,99,0,104,0,2,0,0,164,247,240,62,0,1,22,28,65,1,0,0,0,0,0,0,0,6,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,1,22,28,65,29,0,0,0,70,252,240,62,34,0,0,0,255,255,1,0,24,0,109,111,69,110,100,70,97,99,101,51,73,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,38,0,0,0,165,255,240,62,1,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,29,0,0,0,70,252,240,62,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0";
 
            StringtoArray(str3, ref var20);
            pDisp3 = Part.Extension.GetObjectByPersistReference3((var20), out longstatus);
 
            StringtoArray(str4, ref var21);
            pDisp4 = Part.Extension.GetObjectByPersistReference3((var21), out longstatus);
 
            varArray1[0] = pDisp3;
            varArray2[0] = pDisp4;
 
            lrMngr = Study.LoadsAndRestraintsManager;
            lrMngr.DeleteLoadsAndRestraints("pin between branches");
 
            // Add pin connector
            pinConnector = lrMngr.AddPinConnector((varArray1), (varArray2), ref errCode);
            pinConnector.PinConnectorBeginEdit();
            Debug.Print("Number of entities in Pin Connector-3");
            Debug.Print("  First location: " + pinConnector.GetFirstLocationEntityCount());
            Debug.Print("  Second location: " + pinConnector.GetSecondLocationEntityCount());
            string libName = null;
            string materialName = null;
            pinConnector.GetLibraryMaterial(out libName, out materialName);
            Debug.Print("Library: " + libName);
            Debug.Print("Library material: " + materialName);
            Debug.Print("Axial stiffness: " + pinConnector.AxialStiffnessValue);
            Debug.Print("Include mass? " + pinConnector.IncludeMass);
            Debug.Print("Include strength data? " + pinConnector.IncludeStrengthData);
            Debug.Print("Prevent relative rotation between cylindrical faces? " + pinConnector.IncludeTypeWithKey);
            Debug.Print("Prevent relative axial translation between cylindrical faces? " + pinConnector.IncludeTypeWithRetainerRing);
            Debug.Print("Mass: " + pinConnector.MassValue);
            Debug.Print("Source of pin material (0=library, 1=user-defined): " + pinConnector.MaterialType);
            Debug.Print("Strength units as defined in swsStrengthUnit_e: " + pinConnector.PinStrengthUnit);
            Debug.Print("Strength: " + pinConnector.PinStrengthValue);
            Debug.Print("Rotational stiffness: " + pinConnector.RotationalStiffnessValue);
            Debug.Print("Safety factor: " + pinConnector.SafetyFactor);
            Debug.Print("Tensile stress area unit as defined in swsTensileStressAreaUnit_e: " + pinConnector.TensileStressAreaUnit);
            Debug.Print("Tensile stress area: " + pinConnector.TensileStressAreaValue);
            Debug.Print("Units for axial and rotational stiffness as defined in swsUnit_e: " + pinConnector.Unit);
            pinConnector.PinConnectorEndEdit();
 
            // Mesh model
            CwMesh = Study.Mesh;
            if (CwMesh == null)
                ErrorMsg(swApp, "Failed to get mesh object"false);
 
            CwMesh.Quality = 1;
            CwMesh.GetDefaultElementSizeAndTolerance((int)swsLinearUnit_e.swsLinearUnitMillimeters, out el, out tl);
 
            errCode = Study.CreateMesh((int)swsLinearUnit_e.swsLinearUnitMillimeters, el, tl);
            if (errCode != 0)
                ErrorMsg(swApp, "Failed to create mesh"true);
 
            // Run analysis
            errCode = Study.RunAnalysis();
            if (errCode != 0)
                ErrorMsg(swApp, "Analysis failed"true);
 
 
        }
 
 
        private void StringtoArray(string inputSTR, ref byte[] varEntity)
        {
            string[] PIDArray = null;
            byte[] PID = null;
            int i;
 
            // Parse string into an array
            PIDArray = inputSTR.Split(new char[] { ',' });
 
            //Convert string array to byte array
            int sizeArray = PIDArray.Length;
            PID = new byte[sizeArray];
            for (i = 0; i < PIDArray.Length; i++)
            {
 
                PID[i] = Convert.ToByte(PIDArray[i]);
            }
            varEntity = PID;
        }
 
 
 
 
        public void ErrorMsg(SldWorks SwApp, string Message, bool EndTest)
        {
            SwApp.SendMsgToUser2(Message, 0, 0);
            SwApp.RecordLine("'*** WARNING - General");
            SwApp.RecordLine("'*** " + Message);
            SwApp.RecordLine("");
 
 
            if (EndTest)
            {
                SwApp.ExitApp();
            }
 
        }
 
 
 
 
        /// <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:   Add Pin Connector 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) 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.