怡宁塑胶模具设计

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C++ 2010

 

 //获取UI值和路径
void CreateNewComponent::GetUIValueandPath()
 {
    try
    {
  NewName.clear();
  NewNamePath.clear();
  selectBodys.clear();

  //获取显示部件的全路径
  partfullpath1 = basedisplaypart->FullPath();
  //theSession->ListingWindow()->WriteLine("显示部件全路径:"+partfullpath1);

  //分割字符串得到显示部件名
  string basedisplaypartName = NameOrPath(partfullpath1.GetLocaleText(), 1);
  //theSession->ListingWindow()->WriteLine("显示部件的名字:"+basedisplaypartName);

  //分割字符串得到模号
  int sta = (int)basedisplaypartName.find_first_of("-");//找到工作部件全路径名中的最后一个杠
  displaypartMoldNo = basedisplaypartName.substr(0, sta);//分割
  //theSession->ListingWindow()->WriteLine("模号:"+displaypartMoldNo);

  //分割字符串得到显示部件路径
  PartPath1 = NameOrPath(partfullpath1.GetLocaleText(), 2);
  //theSession->ListingWindow()->WriteLine("显示部件路径:"+PartPath1);

  //获取模号
  PropertyList *stringMoldNoProps = stringMoldNo->GetProperties();
  NXString theMoldNo = stringMoldNoProps->GetString("Value");
  delete stringMoldNoProps;
  stringMoldNoProps = NULL; 

  //获取工件号
  PropertyList *stringNameProps = stringName->GetProperties();
  NXString theName = stringNameProps->GetString("Value");
  delete stringNameProps;
  stringNameProps = NULL; 

  //获取起始工件号
  PropertyList *stringBeginNameProps = stringBeginName->GetProperties();
  NXString theBeginName = stringBeginNameProps->GetString("Value");
  delete stringBeginNameProps;
  stringBeginNameProps = NULL; 

  //获取选择控件选择的体
  selectBodys = selection0->GetSelectedObjects(); //得选择的体


  //获取枚举值
  int enum0Value = enum0->GetProperties()->GetEnum("Value");

  //转换
  char NewMoldNo1[256];
  sprintf(NewMoldNo1, "%s", theMoldNo.GetLocaleText()); //NXString字符串转char,必须用.GetLocaleText()
  string NewMoldNo2 = NewMoldNo1;

  //
  if ( enum0Value == 0 )
  {
      //转换

   char NewName1[256];
   sprintf(NewName1, "-%s", theName.GetLocaleText()); //NXString字符串转char,必须用.GetLocaleText()
   string NewName2 = NewName1;
   char NewName3[256];
   sprintf(NewName3, "\\%s-%s.prt",NewMoldNo2, theName.GetLocaleText()); //NXString字符串转char,必须用.GetLocaleText()
   string NewName4 = NewName3;
   //新建部件的名字
   string NewpartName1 = NewMoldNo2 + NewName2;
   NewName.push_back(NewpartName1);
   //theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
   //新建部件全路径
   string NewNamePath1 = PartPath1 + NewName4;
   NewNamePath.push_back(NewNamePath1);
   //theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
  }
  else if ( enum0Value == 1 )
  {

   string theBeginNamestring1 = theBeginName.GetLocaleText() ;
   int start1 = (int)theBeginNamestring1.find_last_of("-");//找到工作部件全路径名中的最后一个杠
   int end1 = (int)theBeginNamestring1.find_last_of("");//找到工作部件全路径名中的最后一个空格
   string theBeginNamestring2 = theBeginNamestring1.substr(start1+1, end1);//分割
   string theBeginNamestring3 = theBeginNamestring1.substr(0, start1+1);//分割
   int beginNo = atoi(theBeginNamestring2.c_str());//字符串转为整数型

   for ( int i = 0 ; i < selectBodys.size();i++)
   {
    if ( beginNo < 10 )
    {
     //转换
     char NewName1[256];
     sprintf(NewName1, "-%s00%d", theBeginNamestring3,beginNo); //NXString字符串转char,必须用.GetLocaleText()
     string NewName2 = NewName1;
     char NewName3[256];
     sprintf(NewName3, "\\%s-%s00%d.prt",NewMoldNo2, theBeginNamestring3,beginNo); //NXString字符串转char,必须用.GetLocaleText()
     string NewName4 = NewName3;
     //新建部件的名字
     string NewpartName1 = NewMoldNo2 + NewName2;
     NewName.push_back(NewpartName1);
     //theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
     //新建部件全路径
     string NewNamePath1 = PartPath1 + NewName4;
     NewNamePath.push_back(NewNamePath1);
     //theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
     beginNo++;
    }
    else if ( beginNo  >= 10 && beginNo < 100 )
    {
     //转换
     char NewName1[256];
     sprintf(NewName1, "-%s0%d", theBeginNamestring3,beginNo); //NXString字符串转char,必须用.GetLocaleText()
     string NewName2 = NewName1;
     char NewName3[256];
     sprintf(NewName3, "\\%s-%s0%d.prt",NewMoldNo2, theBeginNamestring3,beginNo); //NXString字符串转char,必须用.GetLocaleText()
     string NewName4 = NewName3;
     //新建部件的名字
     string NewpartName1 = NewMoldNo2 + NewName2;
     NewName.push_back(NewpartName1);
     //theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
     //新建部件全路径
     string NewNamePath1 = PartPath1 + NewName4;
     NewNamePath.push_back(NewNamePath1);
     //theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
     beginNo++;
    }
    else if ( beginNo  >= 100 )
    {
     //转换
     char NewName1[256];
     sprintf(NewName1, "-%s%d", theBeginNamestring3,beginNo); //NXString字符串转char,必须用.GetLocaleText()
     string NewName2 = NewName1;
     char NewName3[256];
     sprintf(NewName3, "\\%s-%s%d.prt",NewMoldNo2, theBeginNamestring3,beginNo); //NXString字符串转char,必须用.GetLocaleText()
     string NewName4 = NewName3;
     //新建部件的名字
     string NewpartName1 = NewMoldNo2 + NewName2;
     NewName.push_back(NewpartName1);
     //theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
     //新建部件全路径
     string NewNamePath1 = PartPath1 + NewName4;
     NewNamePath.push_back(NewNamePath1);
     //theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
     beginNo++;    
    }
   }
  }
  else if ( enum0Value == 2 )
  {
   //新建部件的名字
   string NewpartName1 = NewMoldNo2 + "-Layout";
   NewName.push_back(NewpartName1);
   //theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
   //新建部件全路径
   char NewName3[256];
   sprintf(NewName3, "\\%s-Layout.prt",NewMoldNo2); //NXString字符串转char,必须用.GetLocaleText()
   string NewNamePath1 = PartPath1 + NewName3;
   NewNamePath.push_back(NewNamePath1);
   //theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);

   //新建部件的名字
   string NewpartName2 = NewMoldNo2 + "-Misc";
   NewName.push_back(NewpartName2);
   //theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName2 );
   //新建部件全路径
   char NewName4[256];
   sprintf(NewName4, "\\%s-Misc.prt",NewMoldNo2); //NXString字符串转char,必须用.GetLocaleText()
   string NewNamePath2 = PartPath1 + NewName4;
   NewNamePath.push_back(NewNamePath2);
   //theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath2);

   //新建部件的名字
   string NewpartName3 = NewMoldNo2 + "-Ejector";
   NewName.push_back(NewpartName3);
   //theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName3 );
   //新建部件全路径
   char NewName5[256];
   sprintf(NewName5, "\\%s-Ejector.prt",NewMoldNo2); //NXString字符串转char,必须用.GetLocaleText()
   string NewNamePath3 = PartPath1 + NewName5;
   NewNamePath.push_back(NewNamePath3);
   //theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath3);

   //新建部件的名字
   string NewpartName4 = NewMoldNo2 + "-Cavity";
   NewName.push_back(NewpartName4);
   //theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName4 );
   //新建部件全路径
   char NewName6[256];
   sprintf(NewName6, "\\%s-Cavity.prt",NewMoldNo2); //NXString字符串转char,必须用.GetLocaleText()
   string NewNamePath4 = PartPath1 + NewName6;
   NewNamePath.push_back(NewNamePath4);
   //theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath4);

   //新建部件的名字
   string NewpartName5 = NewMoldNo2 + "-Core";
   NewName.push_back(NewpartName5);
   //theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName5 );
   //新建部件全路径
   char NewName7[256];
   sprintf(NewName7, "\\%s-Core.prt",NewMoldNo2); //NXString字符串转char,必须用.GetLocaleText()
   string NewNamePath5 = PartPath1 + NewName7;
   NewNamePath.push_back(NewNamePath5);
   //theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath5);
  }
  
  //遍历文件夹和内存
  //filesearch(strDir, 0); 

 
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreateNewComponent::theUI->NXMessageBox()->Show("获取UI值和路径", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }

 }

 //同名对比
void CreateNewComponent::ToCompareSameName()
 {
    try
    {
  //遍历内存
  Cylmemory();
  //遍历文件夹
  filesearch(strDir, 0);

  ispartnamecreate = false ;

  for(int j = 0 ; j < samepartname1.size();j++)
  {
   string currPartName2 = samepartname1[j];

   for (int Num = 0 ; Num < NewName.size();Num++)
   {
    string newname3 = NewName [Num];

    if (currPartName2 == newname3)
    {
     ispartnamecreate = true ;
     ALLsamenamePart.push_back(newname3);
     //theSession->ListingWindow()->WriteLine(newname3+"  部件已存在请重新输入工件名或起始工件名");
     //theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, newname3+"部件已存在");
    }
    else
    {
     for ( int i = 0 ; i<samepartname2.size();i++)
     {
      string currPartName3 = samepartname2[i];
      if (currPartName3 == newname3)
      {
       ispartnamecreate = true ;
       ALLsamenamePart.push_back(newname3);
       //theSession->ListingWindow()->WriteLine(newname3+"  部件已存在请重新输入工件名或起始工件名");
       //theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, newname3+"部件已存在");
      }    
     }

    }
   }

  }


    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreateNewComponent::theUI->NXMessageBox()->Show("同名对比", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }

 }

//遍历内存
void CreateNewComponent::Cylmemory()
 {
    try
    {

  //通过迭代器获取内存加载部件的名
  samepartname1.clear();
  PartCollection *Pc = theSession->Parts();
  for ( PartCollection::iterator ite = Pc->begin(); ite !=Pc->end(); ite++)
  {
   Part *part = (*ite);
   NXString partfullpath2 = part ->FullPath();
   //theSession->ListingWindow()->WriteLine("部件路径:"+partfullpath2);
   //分割字符串得到内存加载的部件名字
   string currPartName1 = NameOrPath(partfullpath2.GetLocaleText(), 1);
   //新建的部件名字和内存加载部件的名字对比,相同就把内存加载部件的名字塞到容器
   for (int Num = 0 ; Num < NewName.size();Num++)
   {
    string newname1 = NewName [Num];

    if (currPartName1 == newname1)
    {
     samepartname1.push_back(currPartName1);
     //theSession->ListingWindow()->WriteLine("同名部件:"+currPartName1);
    }   
   }

  }

    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreateNewComponent::theUI->NXMessageBox()->Show("遍历内存", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }

 }

 //获取文件名和路径
string CreateNewComponent::NameOrPath(const char* Path, int Type)
 {
    try
    {
   //反向找位置,分割字符串(只读取文件夹路径)
   string strPath = Path;
   string strDir;
   int nPos = strPath.find_last_of('\\');
   if (string::npos != nPos)
   {
    strDir = strPath.substr(0, nPos);
   }
 
   //分割字符串(只读取part名字+后缀)
   //方法2
   int pos = strPath.find_last_of('\\');
   string s1(strPath.substr(pos + 1));
 
   //分割字符串(只读取part名)
   string PartName(s1.substr(0, s1.find(".prt")));

   if (Type == 1)
   {
    return PartName;//返回名字
   }
   else if (Type == 2)
   {
    return strDir;//返回文件夹路径
   }

    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreateNewComponent::theUI->NXMessageBox()->Show("获取文件名和路径", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }


 }

//创建单个部件
void CreateNewComponent::CreateSinglPart()
 {
    try
    {

  for ( int i = 0 ; i < NewName.size(); i++)
  {
   //新建部件的名字
      string NewNamestr = NewName[i];
   //新建部件全路径
   string NewNamePathstr = NewNamePath[i];
   //创建部件
   FileNew *fileNew1;
   fileNew1 = theSession->Parts()->FileNew();
   fileNew1->SetTemplateFileName("model-plain-1-mm-template.prt");
   fileNew1->SetApplication(FileNewApplicationModeling);
   fileNew1->SetUnits(Part::UnitsMillimeters);
   fileNew1->SetRelationType("");
   fileNew1->SetUsesMasterModel("No");
   fileNew1->SetTemplateType(FileNewTemplateTypeItem);
   fileNew1->SetNewFileName(NewNamePathstr);//设置新建部件的路径
   fileNew1->SetMasterFileName("");
   fileNew1->SetUseBlankTemplate(false);
   fileNew1->SetMakeDisplayedPart(false);
   Assemblies::CreateNewComponentBuilder *createNewComponentBuilder1;
   createNewComponentBuilder1 = workPart->AssemblyManager()->CreateNewComponentBuilder();
   //createNewComponentBuilder1->SetReferenceSet(Assemblies::CreateNewComponentBuilder::ComponentReferenceSetTypeOther);
   createNewComponentBuilder1->SetReferenceSet(Assemblies::CreateNewComponentBuilder::ComponentReferenceSetTypeModel);//引用集设置
   createNewComponentBuilder1->SetReferenceSetName("MODEL");//设置引用集名字
   createNewComponentBuilder1->SetNewComponentName(NewNamestr);//设置新建部件的名字

   //把选择的体塞到objects1
   std::vector<DisplayableObject *> objects1(selectBodys.size());
   for (int j = 0 ; j < selectBodys.size(); j++ )
   {
    Body *body1(dynamic_cast<Body *>(NXObjectManager::Get(selectBodys[j]->Tag()))); //选择的目标转换body
    objects1[j] = body1;
   }
  
   bool added1;
   added1 = createNewComponentBuilder1->ObjectForNewComponent()->Add(objects1);
   
   createNewComponentBuilder1->SetNewFile(fileNew1);
   
   NXObject *nXObject1;
   nXObject1 = createNewComponentBuilder1->Commit();

   //fileNew1->Destroy();
   
   createNewComponentBuilder1->Destroy();
  
  }

    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreateNewComponent::theUI->NXMessageBox()->Show("创建单个部件", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }

 }

//批量创建部件
void CreateNewComponent::CreateMultiplePart()
 {
    try
    {

  for ( int i = 0 ; i < NewName.size(); i++)
  {
   //新建部件的名字
      string NewNamestr = NewName[i];
   //新建部件全路径
   string NewNamePathstr = NewNamePath[i];
   //创建部件
   FileNew *fileNew1;
   fileNew1 = theSession->Parts()->FileNew();
   fileNew1->SetTemplateFileName("model-plain-1-mm-template.prt");
   fileNew1->SetApplication(FileNewApplicationModeling);
   fileNew1->SetUnits(Part::UnitsMillimeters);
   fileNew1->SetRelationType("");
   fileNew1->SetUsesMasterModel("No");
   fileNew1->SetTemplateType(FileNewTemplateTypeItem);
   fileNew1->SetNewFileName(NewNamePathstr);//设置新建部件的路径
   fileNew1->SetMasterFileName("");
   fileNew1->SetUseBlankTemplate(false);
   fileNew1->SetMakeDisplayedPart(false);
   Assemblies::CreateNewComponentBuilder *createNewComponentBuilder1;
   createNewComponentBuilder1 = workPart->AssemblyManager()->CreateNewComponentBuilder();
   //createNewComponentBuilder1->SetReferenceSet(Assemblies::CreateNewComponentBuilder::ComponentReferenceSetTypeOther);
   createNewComponentBuilder1->SetReferenceSet(Assemblies::CreateNewComponentBuilder::ComponentReferenceSetTypeModel);//引用集设置
   createNewComponentBuilder1->SetReferenceSetName("MODEL");//设置引用集名字
   createNewComponentBuilder1->SetNewComponentName(NewNamestr);//设置新建部件的名字
   //把选择的体塞到objects1
   std::vector<DisplayableObject *> objects1(1);
   Body *body1(dynamic_cast<Body *>(NXObjectManager::Get(selectBodys[i]->Tag()))); //选择的目标转换body
   objects1[0] = body1;
   bool added1;
   added1 = createNewComponentBuilder1->ObjectForNewComponent()->Add(objects1);
   
   createNewComponentBuilder1->SetNewFile(fileNew1);
   
   NXObject *nXObject1;
   nXObject1 = createNewComponentBuilder1->Commit();

   //fileNew1->Destroy();
  
   createNewComponentBuilder1->Destroy();

  
  }

    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreateNewComponent::theUI->NXMessageBox()->Show("批量创建部件", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }

 }

//新模装配树
void CreateNewComponent::CreateNewAssmPart()
 {
    try
    {

  //获取UI值和路径
  GetUIValueandPath();

  for ( int i = 0 ; i < NewName.size(); i++)
  {
   //新建部件的名字
      string NewNamestr = NewName[i];
   //新建部件全路径
   string NewNamePathstr = NewNamePath[i];
   //创建部件
   FileNew *fileNew1;
   fileNew1 = theSession->Parts()->FileNew();
   fileNew1->SetTemplateFileName("model-plain-1-mm-template.prt");
   fileNew1->SetApplication(FileNewApplicationModeling);
   fileNew1->SetUnits(Part::UnitsMillimeters);
   fileNew1->SetRelationType("");
   fileNew1->SetUsesMasterModel("No");
   fileNew1->SetTemplateType(FileNewTemplateTypeItem);
   fileNew1->SetNewFileName(NewNamePathstr);//设置新建部件的路径
   fileNew1->SetMasterFileName("");
   fileNew1->SetUseBlankTemplate(false);
   fileNew1->SetMakeDisplayedPart(false);

   NXObject *nXObject1;
   nXObject1 = fileNew1->Commit();
   
   fileNew1->Destroy();

   //把新文件装配到显示部件
   Point3d basePoint1(0.0, 0.0, 0.0);
   Matrix3x3 orientation1;
   orientation1.Xx = 1.0;
   orientation1.Xy = 0.0;
   orientation1.Xz = 0.0;
   orientation1.Yx = 0.0;
   orientation1.Yy = 1.0;
   orientation1.Yz = 0.0;
   orientation1.Zx = 0.0;
   orientation1.Zy = 0.0;
   orientation1.Zz = 1.0;
   PartLoadStatus *partLoadStatus2;
   Assemblies::Component *component1;
   component1 = workPart->ComponentAssembly()->AddComponent(NewNamePathstr, "None", NewNamestr, basePoint1, orientation1, -1, &partLoadStatus2, true);
   
   delete partLoadStatus2;


  }

    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreateNewComponent::theUI->NXMessageBox()->Show("新模装配树", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }

 }

//保存新建部件
void CreateNewComponent::SaveNewPart()
 {
    try
    {
  
  PartSaveStatus *partSaveStatus1;
  for ( int i = 0 ; i < NewName.size(); i++)
  {
   Part *part1(dynamic_cast<Part *>(theSession->Parts()->FindObject(NewName[i])));
   string savenewname = NewNamePath[i];
   partSaveStatus1 = part1->Save(BasePart::SaveComponentsTrue, BasePart::CloseAfterSaveFalse);
  
  }

     delete partSaveStatus1;

  //保存当前显示部件
  //PartSaveStatus *partSaveStatus2;
  //partSaveStatus2 = basedisplaypart->Save(BasePart::SaveComponentsTrue, BasePart::CloseAfterSaveFalse);
  //delete partSaveStatus2;


    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreateNewComponent::theUI->NXMessageBox()->Show("新模装配树", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }

 }

 

posted on 2021-02-27 16:55  怡宁塑胶模具设计  阅读(309)  评论(0编辑  收藏  举报