2 Replies Latest reply on Jan 11, 2016 5:20 PM by Tony Lissington

    How do I apply a custom orthotropic material using simulation API?

    Tony Lissington

      Hi.

       

      I am trying to set a (thermal) simulation which uses orthotropic materials (conductivity is different in different directions). I try to apply a custom material to individual parts and setting the reference geometry, but it doesn't appear to be working.

       

      Has anyone else been able to do this successfully?

       

      Thanks in advance for any help.

        • Re: How do I apply a custom orthotropic material using simulation API?
          Peer Larson

          Hi Tony,

           

          Couldn't say specifically, but perhaps if you could provide some code snippets and info on any debugging you've done we may be able to progress further.

           

          Peer

            • Re: How do I apply a custom orthotropic material using simulation API?
              Tony Lissington

              Hi Peer

               

              Thanks for your comment.  Below is the complete code we are using so far.  Almost everything is working well, The code sets the material to a custom orthotropic material, and enters the different values for thermal conductivity in x,y,z.  Where it goes wrong is that it names the direction reference, but doesn't actually set the selected axis plane or set the x, y, z directions relative to this (x, y, and z appear to line up the the assembly coordinate system).

               

              Thanks,

              Tony

               

               

              using SolidWorks.Interop.sldworks;

              using SolidWorks.Interop.swconst;

              using System.Runtime.InteropServices;

              using System;

              using SolidWorks.Interop.cosworks;

              using System.Diagnostics;

               

              namespace ApplyOrthotropicMaterials.csproj

              {

                  public partial class SolidWorksMacro

                  {

                      public void Main()

                      {

                          object COSMOSWORKS = null;

                          CwAddincallback COSMOSObject = null;

                          CWModelDoc ActDoc = null;

                          CWStudyManager StudyMngr = null;

                          CWStudy Study = null;

                          CWSolidManager SolidMgr = null;

                          CWSolidComponent SolidComponent = null;

                          CWSolidBody SolidBody = null;

                          int errorCode = 0;

                          int errCode = 0;

                          int CompCount = 0;

                          CWMaterial CWMat = null;

               

                          // get a reference to the solidworks doc for selcting bodies etc.

                          ModelDoc2 swModel = null;

                          SelectionMgr swSelMgr = null;

               

                          // Get SOLIDWORKS Simulation object>

                          COSMOSObject = (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");

                          if (COSMOSObject == null)

                              throw new ArgumentNullException("COSMOSObject object not found.");

               

                          COSMOSWORKS = COSMOSObject.CosmosWorks;

                          if (COSMOSWORKS == null)

                              throw new ArgumentNullException("COSMOSWORKS object not found.");

               

                          ActDoc = COSMOSObject.CosmosWorks.ActiveDoc;

                          if (ActDoc == null)

                              throw new ArgumentNullException("No active document.");

               

                          StudyMngr = ActDoc.StudyManager;

                          if (StudyMngr == null)

                              throw new ArgumentNullException("StudyMngr object not there.");

               

                          // select the existing study

                          Study = StudyMngr.GetStudy(StudyMngr.ActiveStudy);

               

                          if (Study == null)

                              throw new ArgumentNullException("Study does not exist.");

               

                          //Get number of solid components

                          SolidMgr = Study.SolidManager;

               

                          if (SolidMgr == null)

                              throw new ArgumentNullException("SolidManager object not created.");

               

                          CompCount = SolidMgr.ComponentCount;

               

                          //Apply a different SOLIDWORKS Simulation library material to rest of components

               

                          SolidBody = null;

                          SolidComponent = null;

               

                          String overallAssemblyName = "386-A1";

               

                          // go through and select each component one at a time.

                          for (int j = 0; j < CompCount; j++)

                          {

                              SolidComponent = SolidMgr.GetComponentAt(j, out errorCode);

                              String fullyQualifiedPartName = SolidComponent.ComponentName;

                              Char heirarchyDelimiter = '/';

                              Char nameDelimiter = '-';

               

                              // the full name can be nested due to sub-assemblies.

                              String[] nestedName = fullyQualifiedPartName.Split(heirarchyDelimiter);

               

                              // the first nested name is the first assembly

                              String[] assemblyNameParts = nestedName[0].Split(nameDelimiter);

                              String assemblyName = assemblyNameParts[0] + "-" + assemblyNameParts[1];

                              String indexedAssemblyName = assemblyName + "-" + assemblyNameParts[2];

               

                              // the final nested name is the part name

                              String[] partNameParts = nestedName[nestedName.Length - 1].Split(nameDelimiter);

                              String partName = partNameParts[0] + "-" + partNameParts[1];

                              String indexedPartName = partName + "-" + partNameParts[2];

               

                              SolidBody = SolidComponent.GetSolidBodyAt(0, out errCode);

               

                              swModel = ((ModelDoc2)(swApp.ActiveDoc));

                              swSelMgr = (SelectionMgr)swModel.SelectionManager;

               

                              swModel.ClearSelection();

               

                              if (errCode != 0)

                                  throw new ArgumentNullException("No solid body");

               

                              // All Curved Sections

                              if (partName == "386-10" || partName == "386-13" || partName == "386-14" || partName == "386-11" || partName == "386-16")

                              {

                                  // select for visual reference only

                                  //swModel.Extension.SelectByID2(indexedAssemblyName + "@" + overallAssemblyName + "/" + indexedPartName + "@" + assemblyName, "COMPONENT", 0, 0, 0, false, 0, null, 0);

               

                                  // Create the custom Material

                                  CWMat = (CWMaterial)SolidBody.GetSolidBodyMaterial();

                                  SetMaterialToLitzWireCurved(CWMat);

               

                                  // Select the reference plane/ axis for the orthotropic material

                                  //swModel.Extension.SelectByID2("SweepAxis@386-A2-1@386-A1/386-10-1@386-A2", "AXIS", 0, 0, 0, false, 0, null, 0);

               

                                  string referenceName = "MatRef" + "@" + indexedAssemblyName + "@" + overallAssemblyName + "/" + indexedPartName + "@" + assemblyName;

                                  swModel.Extension.SelectByID2(referenceName, "AXIS", 0, 0, 0, false, 0, null, 0);

               

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

                                  RefAxis selectedFeature = (RefAxis)selectedObject.GetSpecificFeature2();

                                  CWMat.SetReferencePlane(selectedObject, out errorCode);

               

                                  // if there was an error then try selecting a plane instead of an axis

                                  if (errorCode != 0)

                                      throw new ArgumentNullException("Need to specify a plane or axis");

               

                                  //now set the solid body material

                                  errCode = SolidBody.SetSolidBodyMaterial(CWMat);

                              }

               

                              // Straight Sections

                              if (partName == "386-12" || partName == "386-17" || partName == "386-15")

                              {

                                  // select for visual reference only

                                  //swModel.Extension.SelectByID2(indexedAssemblyName + "@" + overallAssemblyName + "/" + indexedPartName + "@" + assemblyName, "COMPONENT", 0, 0, 0, false, 0, null, 0);

               

                                  // Create the custom Material

                                  CWMat = (CWMaterial)SolidBody.GetSolidBodyMaterial();

                                  SetMaterialToLitzWireStraight(CWMat);

               

                                  swModel.ClearSelection2(true);

                                  string referenceName = "MatRef" + "@" + indexedAssemblyName + "@" + overallAssemblyName + "/" + indexedPartName + "@" + assemblyName;

                                  swModel.Extension.SelectByID2(referenceName, "PLANE", 0, 0, 0, false, 0, null, 0);

               

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

                                  RefPlane selectedFeature = (RefPlane)selectedObject.GetSpecificFeature2();

                                  CWMat.SetReferencePlane(selectedObject, out errorCode);

               

                                  // if there was an error then try selecting a plane instead of an axis

                                  if (errorCode != 0)

                                      throw new ArgumentNullException("Need to specify a plane or axis");

               

                                  //now set the solid body material

                                  errCode = SolidBody.SetSolidBodyMaterial(CWMat);

                              }

               

                              SolidBody = null;

                          }

                      }

               

                      private void SetMaterialToLitzWireCurved(CWMaterial CWMat)

                      {

                          CWMat.MaterialUnits = 0;

                          CWMat.MaterialName = "LitzWireCurved";

                          CWMat.Category = "Orthotropic";

                          CWMat.ModelType = (int)swsMaterialModelType_e.swsMaterialModelTypeLinearElasticOrthtropic;

                          CWMat.SetPropertyByName("DENS", 8900, 0);

                          CWMat.SetPropertyByName("KX", 1.2, 0);

                          CWMat.SetPropertyByName("KY", 167, 0);

                          CWMat.SetPropertyByName("KZ", 1.2, 0);

                          CWMat.SetPropertyByName("C", 180, 0);

                      }

               

                      private void SetMaterialToLitzWireStraight(CWMaterial CWMat)

                      {

                          CWMat.MaterialUnits = 0;

                          CWMat.MaterialName = "LitzWireStraight";

                          CWMat.Category = "Orthotropic";

                          CWMat.ModelType = (int)swsMaterialModelType_e.swsMaterialModelTypeLinearElasticOrthtropic;

                          CWMat.SetPropertyByName("DENS", 8900, 0);

                          CWMat.SetPropertyByName("KX", 1.2, 0);

                          CWMat.SetPropertyByName("KY", 1.2, 0);

                          CWMat.SetPropertyByName("KZ", 167, 0);

                          CWMat.SetPropertyByName("C", 180, 0);

                      }

               

               

                      /// <summary>

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

                      /// </summary>

                      public SldWorks swApp;

                  }

              }