美文网首页狮猿社CATIA
CATIA VBA 干涉检查

CATIA VBA 干涉检查

作者: 锦囊喵 | 来源:发表于2020-02-05 13:51 被阅读0次

    原文链接
    以下代码使用Clash及Group检查干涉情况:

    Sub CATMain()
    
        ' get root product of document'
        Dim RootProd As Product
        Set RootProd = CATIA.ActiveDocument.Product
    
        ' retrieve selection object of active document'
        Dim objSelection As Selection
        Set objSelection = CATIA.ActiveDocument.Selection
    
        ' get two selected objects'
        If (objSelection.Count2 <> 2) Then
            MsgBox "Before running the script you must select two products to compute clash for", vbOKOnly, "No products selected"
            Exit Sub
        End If
    
        Dim FirstProd As Product
        Dim SecondProd As Product
    
        Set FirstProd = objSelection.Item2(1).Value
        Set SecondProd = objSelection.Item2(2).Value
    
        ' create groups for clash computation'
        Dim objGroups As Groups
        Set objGroups = RootProd.GetTechnologicalObject("Groups")
    
        Dim grpFirst As Group
        Dim grpSecond As Group
    
        Set grpFirst = objGroups.Add()
        Set grpSecond = objGroups.Add()
    
        ' add selected products to groups'
        grpFirst.AddExplicit FirstProd
        grpSecond.AddExplicit SecondProd
    
    
        ' get access to Clashes collection'
        Dim objClashes As Clashes
        Set objClashes = RootProd.GetTechnologicalObject("Clashes")
    
        ' create new clash'
        Dim newClash As Clash
        Set newClash = objClashes.Add()
    
        ' set new clash to be computed between two groups (two selected products)'
        newClash.FirstGroup = grpFirst
        newClash.SecondGroup = grpSecond
    
        newClash.ComputationType = catClashComputationTypeBetweenTwo
    
        ' compute clash'
        newClash.Compute
        For I = 1 To cConflicts.Count
    
            Set oConflict = cConflicts.Item(I)
            
    '        Worksheets("Clash Results").Cells(I + 2, 1).Value = I'
            
            Debug.Print oConflict.FirstProduct.Name
            Debug.Print oConflict.SecondProduct.Name
            Debug.Print oConflict.Value
            Debug.Print oConflict.Type
            Debug.Print oConflict.Status
            Debug.Print oConflict.Comment
        Next
        
    End Sub 
    

    原文链接

    Clash Detection In CATIA

    Introduction

    CATIA支持在装配中对组件进行干涉检测,在许多工作台可以使用干涉分析工具。但分析结果有些令人费解。

    借助CATIA VBA API,自定义一些工具, 可以有效提高工作效率。

    本文将展示通过EXCEL操作CATIA并实现干涉分析。

    使用CATIA进行干涉分析

    通过干涉分析,CATIA可以找到
    *干涉
    *接触
    *距离

    可以对整个模型或者所选模型进行干涉分析,下面展示的是对整个模型进行干涉分析。

    在CATIA里打开装配工作台,执行干涉分析。

    将会执行检查干涉程序,本例中,CATIA将会对当前product内所有part进行检查,以找出在5mm内的干涉与接触。

    CATIA执行结果


    可以保存结果为报表。


    用户可以编辑结果,添加注释,修改状态。


    Catia stores these results in the model. The results can be reviewed later.
    CATIA将在模型内保存检查结果,并且可以review。


    但当再次执行干涉分析,结果如下:

    image.png

    所有自定义内容将会丢失。

    使用VBA进行干涉分析

    The Concept

    1. Open an Excel Workbook

    2. In Excel VBA, create a connection to CATIA

      1. Excel has now got full control of CATIA, via CATIA's API.
    3. Run the CATIA Clash Analysis.

    4. Write the results of the analysis to Excel.

    5. Each row in excel is a clash result between 2 parts.

      1. When each row is selected, put all other parts in no show.

      2. Analyses the clash and fix.

      3. Allow a fresh clash analysis between both parts.

    The Code

    Connecting Excel to CATIA

    In VBA, catia is an ActivX object, so we simple create an object.

    *Function GetCATIA() As Object*
    
    *Set catia = GetObject(, "CATIA.Application")*
    
    *If catia Is Nothing Then*
    
    *Set catia = CreateObject("CATIA.Application")*
    
    *catia.Visible = True*
    
    *End If*
    
    *Set GetCATIA = catia*
    
    *End Function*
    

    In our code then we simple use Set catia = GetCATIA to create an object to the live CATIA session. See http://www.maruf.ca/files/caadoc/CAAScdInfTechArticles/CAAInfInvoking.htm

    Creating the Clash Analysis

    Assuming we have an open catproduct in CATIA then, access the clash technology object :

    *Dim cClashes As Clashes*
    
    *Set cClashes = CATIA.ActiveDocument.Product.GetTechnologicalObject("Clashes")*
    
    Create a new clash analysis
    
    *Dim newClash 'as Clash*
    
    *Set newClash = cClashes.Add()*
    
    Set the clash to check for a clash between all components
    
    *oClash.ComputationType = catClashComputationTypeBetweenAll*
    

    Tell catia to perform the clash

    oClash.Compute

    Analysing the results

    The clash results are all stored in the oClash object. We simply need to iterate through this and write the results to a spreadsheet.

    *Dim cConflicts As Conflicts*
    
    *Dim oConflict As Conflict*
    
    *Set cConflicts = oClash.Conflicts*
    
    *For I = 1 To cConflicts.Count*
    
    *Set oConflict = cConflicts.Item(I)*
    
    *Worksheets("Clash Results").Cells(I + 2, 1).Value = I*
    
    *Worksheets("Clash Results").Cells(I + 2, 2).Value = oConflict.FirstProduct.Name*
    
    *Worksheets("Clash Results").Cells(I + 2, 3).Value = oConflict.SecondProduct.Name*
    
    *Worksheets("Clash Results").Cells(I + 2, 4).Value = oConflict.Value*
    
    *Worksheets("Clash Results").Cells(I + 2, 5).Value = oConflict.Type*
    
    *Worksheets("Clash Results").Cells(I + 2, 6).Value = oConflict.Status*
    
    *Worksheets("Clash Results").Cells(I + 2, 7).Value = oConflict.Comment*
    
    *next*
    

    This will create a spreadsheet like:

    This is similar to the output from running clash detection manually. The purpose of this exercise is to extend the capability of clash analysis.

    Show the results of a single conflict

    We will perform the following tasks on a single clash result when the user double-clicks the row.

    1. Select the clash result
    2. Put All Parts in no show
    3. Put both parts in the clash assembly into a selection and show

    选择干涉对象

    *Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)*
    *On Error GoTo errhandler*
    *Dim intClashNumber As Integer*
    *intClashNumber = Cells(Target.Row, 1) ' Get the clash number*
    *'Highlight the row Give us a visual clue of what we are working on*
    *    If Target.Cells.Count > 1 Then Exit Sub*
    *        Application.ScreenUpdating = False*
    *    ' Clear the color of all the cells*
    *        Cells.Interior.ColorIndex = 0*
    *     With Target*
    *        .EntireRow.Interior.ColorIndex = 8*
    *    End With*
    *    Application.ScreenUpdating = True*
    *Application.DisplayStatusBar = True*
    *Application.StatusBar = "Please be patient,..."*
    *Call cmdShowThisClashAlone(intClashNumber)*
    *Exit Sub*
    *errhandler:*
    *Debug.Print Err.Description*
    *Err.Clear*
    *End Sub*
    

    隐藏所有Part

    *Sub controlPartVisibility(boolVisibility As Boolean)*
    *Dim productDocument1 'As ProductDocument*
    *Dim selection1 'As Selection*
    *Dim visPropertySet1 'As VisPropertySet*
    
    *Set catia = GetCATIA*
    *Set productDocument1 = catia.ActiveDocument*
    *Set selection1 = productDocument1.Selection*
    *Set visPropertySet1 = selection1.VisProperties*
    
    *' boolVisibility if true show all invisible parst*
    *' if false, hide all visible parts*
    *    If boolVisibility Then*
    *        selection1.Search "CATAsmSearch.Part.Visibility=InVisible,all"*
    *        visPropertySet1.SetShow 0*
    *    Else*
    *        selection1.Search "CATAsmSearch.Part.Visibility=Visible,all"*
    *        visPropertySet1.SetShow 1*
    *    End If*
    
    *selection1.Clear*
    
    *Set visPropertySet1 = Nothing*
    *Set selection1 = Nothing*
    *Set productDocument1 = Nothing
    
    End Sub*
    

    选择装配中所有Part并显示

    *Sub cmdShowThisClashAlone(intClashID As Integer)*
    
    *On Error GoTo errhandler*
    *'Put Everthing in No Show*
    *Application.StatusBar = "Putting Every Part in No show, this could take several minutes"*
    
    *controlPartVisibility (False) ' Put all visible parts in no show*
    
    *Dim catia As Object*
    *'Make call to catia*
    
    *Set catia = GetCATIA ' Call CATIA*
    
    *If catia Is Nothing Then*
    
    *MsgBox "I can't connect to CATIA, Is it running?"*
    
    *Exit Sub ' Finish the program*
    
    *End If*
    
    *Dim cClashes 'As Clashes*
    *Set cClashes = catia.ActiveDocument.Product.GetTechnologicalObject("Clashes")*
    
    *Dim newClash 'as Clash*
    
    *Set newClash = cClashes.Item(cClashes.Count) ' Use the last clash*
    
    *Dim cConflicts 'As Conflicts*
    *Set cConflicts = newClash.Conflicts*
    
    *Dim oConflict 'As Conflict*
    *Set oConflict = cConflicts.Item(intClashID)*
    
    *Set productDocument1 = catia.ActiveDocument*
    *Set selection1 = productDocument1.Selection*
    *Set visPropertySet1 = selection1.VisProperties*
    
    *selection1.Clear*
    
    *selection1.Add oConflict.SecondProduct*
    *selection1.Add oConflict.FirstProduct*
    
    *Application.StatusBar = "Showing the clash results"*
    
    *visPropertySet1.SetShow 0*
    
    *Exit Sub*
    
    *errhandler:*
    
    *Err.Clear*
    *Debug.Print Err.Description*
    
    *End Sub*
    

    The Application In Action

    Run the Clash Analysis

    Which creates the CATIA clash

    And writes it to excel

    Double click on a clash and Excel marks the row

    and in catia the clash parts are shown on their own

    Conclusions

    本文简单介绍了如何使用VBA 及Excel对CATIA进行开发。

    相关文章

      网友评论

        本文标题:CATIA VBA 干涉检查

        本文链接:https://www.haomeiwen.com/subject/ovksxhtx.html