使用 catscript 将 .cgr 文件加载到 CATIA 以生成轮廓

问题描述

我正在运行一个 catscript,它导入一个 txt 文件并将 .cgr 文件加载到 catia 中以生成一个 3dxml 文件。当打开这个 3dxml 文件时,所有的 cgr 组件都存在,但产品结构是扁平的,没有可用的多级节点。我已经按照 catia 文档添加组件来生成代码。此代码的目的是生成带有轮廓的零件(完整 cgrs)。代码如下:

  Sub CATMain()
  Dim file_name As String
  Dim RootPartNumber As String
  Dim linecount As Interger
  Dim input_level As Interger
  Dim dbinput_file as File
  Dim arrFields
  Dim dbinput_file_stream  As TextStream
  Dim iMatrix(12)
  Dim oMatrix(11)
  Dim mMatrix(2137,12)
  Dim repo_path(10000) As String
  Dim cgrpath_part(10000,10000) As String
  Dim repo_path_present As Integer
  Dim index As Integer
  Dim Str As String
  Dim ProductArray(10000) As Product
  Dim FileSysObject
  Dim Instance As Product
  Dim file_directory As String
  Dim file_extension As String
  Set FileSysObject = CATIA.FileSystem
  
  flag = false
  file_name=CATIA.SystemService.Environ("SIL_INPUTFLE")
  file_directory="/tmp/DLNAME4/"
  file_extension=".3dxml"
  'CATIA.SystemService.print "Input Argument is  " & file_name
  Dim  value as Boolean
  value=FileSysObject.FileExists(file_name)
   ' MsgBox " return value is  " & value

  If (Not CATIA.FileSystem.FileExists (file_name)) Then
      CATIA.SystemService.print "path not found  "
  End If

  Set dbinput_file = FileSysObject.GetFile(file_name)
  Set dbinput_file_stream = dbinput_file.OpenAsTextStream("ForReading")
  repo_path_present=0
  linecount=1
    

  'Create a new product document object by adding a document with the Product
  'type to the document collection of the CATIA application.

  Dim oProductDoc As Document
  Set oProductDoc = CATIA.Documents.Add("Product")

  'Retrieve the Design part as the root product.
  Dim odesign As Product
  Set odesign = oProductDoc.Product


  'Retrieve the product's collection of the Design.
  Dim odesignProducts As Products
  Set odesignProducts = odesign.Products

  ' Read a line at a time until EOF
  Dim linein As String
  linein = dbinput_file_stream.ReadLine
  do while Not dbinput_file_stream.AtEndOfStream

  ' CATIA.SystemService.print "  value of linein is   " & linein
  arrFields = Split(linein,",")
  ' iMatrix = arrFields
  ' CATIA.SystemService.print "  value of arrFields is   " & arrFields
  'for each x in arrFields
  'For i = Lbound(arrFields) to Ubound(arrFields)

   RootPartNumber=arrFields(0)
   odesign.PartNumber=RootPartNumber
   odesign.Name = RootPartNumber


    iMatrix(0) = CDbl(arrFields(1))
    iMatrix(1) = CDbl(arrFields(2))
    iMatrix(2) = CDbl(arrFields(3))
    iMatrix(3) = CDbl(arrFields(4))
    iMatrix(4) = CDbl(arrFields(5))
    iMatrix(5) = CDbl(arrFields(6))
    iMatrix(6) = CDbl(arrFields(7))
    iMatrix(7) = CDbl(arrFields(8))
    iMatrix(8) = CDbl(arrFields(9))
    iMatrix(9) = CDbl(arrFields(10))
    iMatrix(10) =CDbl(arrFields(11))
    iMatrix(11) =CDbl(arrFields(12))
    iMatrix(12) =CDbl(arrFields(13))

' Here we are storing the cgr path is an Array repo_path and if the cgr already exsists in the array its not added then

   If linecount=1 then
     repo_path(linecount-1)=arrFields(15)
     reDim repoList(0)
     repoList(0) = arrFields(15)
   End If
   If linecount >1 then
       'CATIA.SystemService.print "looking for " & arrFields(15)
       ' CATIA.SystemService.print " line_count is " &linecount
        For index=0 to linecount-1
          If arrFields(15)=repo_path(index) then
            repo_path_present=1
            'CATIA.SystemService.print " path found  with index vlaue " & index
            Exit For
          Else
            'CATIA.SystemService.print " else "
             If linecount-1-index=1 then
              'CATIA.SystemService.print " new cgr with kvlaue " & index
               repo_path_present=0
               repo_path(index+1)=arrFields(15)
              ' CATIA.SystemService.print " new cgr with n pos " & index+1
              Exit For
            Else
              ' CATIA.SystemService.print " e1"
            End If
          End if

        Next
    End if

   flag = false
   input_level=iMatrix(0)

   If input_level=1 Then
    For j = 1 to Ubound(iMatrix)
        mMatrix(input_level,j)=iMatrix(j)
    Next
  Else
   mMatrix(input_level,1) = (mMatrix(input_level-1,1) * iMatrix(1)) + (mMatrix(input_level-1,4) * iMatrix(2)) + (mMatrix(input_level-1,7) * iMatrix(3))
   mMatrix(input_level,2) = (mMatrix(input_level-1,2) * iMatrix(1)) + (mMatrix(input_level-1,5) * iMatrix(2)) + (mMatrix(input_level-1,8) * iMatrix(3))
   mMatrix(input_level,3) = (mMatrix(input_level-1,3) * iMatrix(1)) + (mMatrix(input_level-1,6) * iMatrix(2)) + (mMatrix(input_level-1,9) * iMatrix(3))
   mMatrix(input_level,4) = (mMatrix(input_level-1,1) * iMatrix(4)) + (mMatrix(input_level-1,4) * iMatrix(5)) + (mMatrix(input_level-1,7) * iMatrix(6))
   mMatrix(input_level,5) = (mMatrix(input_level-1,2) * iMatrix(4)) + (mMatrix(input_level-1,5) * iMatrix(5)) + (mMatrix(input_level-1,8) * iMatrix(6))
   mMatrix(input_level,6) = (mMatrix(input_level-1,3) * iMatrix(4)) + (mMatrix(input_level-1,6) * iMatrix(5)) + (mMatrix(input_level-1,9) * iMatrix(6))
   mMatrix(input_level,7) = (mMatrix(input_level-1,1) * iMatrix(7)) + (mMatrix(input_level-1,4) * iMatrix(8)) + (mMatrix(input_level-1,7) * iMatrix(9))
   mMatrix(input_level,8) = (mMatrix(input_level-1,2) * iMatrix(7)) + (mMatrix(input_level-1,5) * iMatrix(8)) + (mMatrix(input_level-1,8) * iMatrix(9))
   mMatrix(input_level,9) = (mMatrix(input_level-1,3) * iMatrix(7)) + (mMatrix(input_level-1,6) * iMatrix(8)) + (mMatrix(input_level-1,9) * iMatrix(9))
   mMatrix(input_level,10) =(mMatrix(input_level-1,1) * iMatrix(10)) + (mMatrix(input_level-1,4) * iMatrix(11)) + (mMatrix(input_level-1,7) * iMatrix(12) + mMatrix(input_level-1,10))
   mMatrix(input_level,11) =(mMatrix(input_level-1,2) * iMatrix(10)) + (mMatrix(input_level-1,5) * iMatrix(11)) + (mMatrix(input_level-1,8) * iMatrix(12) + mMatrix(input_level-1,11))
   mMatrix(input_level,12) =(mMatrix(input_level-1,3) * iMatrix(10)) + (mMatrix(input_level-1,6) * iMatrix(11)) + (mMatrix(input_level-1,9) * iMatrix(12) + mMatrix(input_level-1,12))

  End If
     ' MsgBox "  value of linecount  is   " & linecount
     ' linecount = linecount + 1

   for j = 1 to 12
   oMatrix(j-1) = mMatrix(input_level,j)
   Next
  
   ' If repo_path_present value is set to 0 then the current entry is a new product
        If repo_path_present=0 then
        ' CATIA.SystemService.print " New product "
          Dim oSIL_Prod As Product
         If InStr(arrFields(14),"/") <> 0 Then
           arrFields(14)=Replace(arrFields(14),"/","-")
         End if
            Set  oSIL_Prod = odesignProducts.AddNewProduct(arrFields(14))
            oSIL_Prod.PartNumber = arrFields(14)
            oSIL_Prod.Name = linecount

             'Add a master shape representation to the hull using an existing part and
             'reframe the viewer.
             oSIL_Prod.AddMasterShapeRepresentation arrFields(15)
            ' CATIA.ActiveWindow.ActiveViewer.Reframe
             oSIL_Prod.Move.Apply oMatrix
        End If
   'copy the product reference is a array ProductArray 
        Set ProductArray(linecount-1)=oSIL_Prod.ReferenceProduct

   ' If repo_path_present value is set to 1 then the current entry is already present. Use Add component

        If repo_path_present=1 then
            ' CATIA.SystemService.print " New component "
             'CATIA.SystemService.print " found  with index vlaue " & index
             Set Instance= ProductArray(index)
             'Dim ProductInstance As Product
             Set ProductInstance = odesignProducts.AddComponent(Instance)
             ProductInstance.Name = linecount
             ProductInstance.Move.Apply oMatrix
             'CATIA.SystemService.print " ProductArray value is " & ProductArray(linecount-1)
        End If

     linecount= linecount + 1
     linein = dbinput_file_stream.ReadLine
  Loop
  ' Close file
  dbinput_file_stream.Close

   'Save as 3dxml file
   'oProductDoc.SaveAs file_directory & odesign.PartNumber & file_extension
    oProductDoc.ExportData file_directory & odesign.PartNumber,"3dxml"
End Sub

打开 3dxml 文件时,它不应具有扁平结构,而应具有缩进节点。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)