Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catia Macro : To Create 100 Datum Lines about Center Point

Status
Not open for further replies.

jagandeep

Automotive
May 27, 2013
82
IN
Hello Friends
I want to create 100 Datum lines about Axis System Center in a Given Geometric Set. So each of the line must be exactly 3.6 deg from its neighbor. Unfortunately this is not the case in case of the code I have created. There is a little bit of inaccuracy due to deg to Rad conversion I guess. Is there any better way to do that so that accuracy in deg can be maintained ? Below is my code

Code:
Option Explicit

Sub CATMain()

    Dim MyPartDoc As PartDocument
    Dim MyPart As Part
    Dim MySelection 'As Selection
    
    Set MyPartDoc = CATIA.ActiveDocument
    Set MyPart = MyPartDoc.Part
    Set MySelection = MyPartDoc.Selection
    
    Dim vFilter1(0)
    Dim Status As String
    
    vFilter1(0) = "HybridBody"
    
    MySelection.Clear
    Status = MySelection.SelectElement2(vFilter1, "Select Geometric Set", True)
    
    If Status = "Normal" Then
    
        Dim MyHybridBody As HybridBody
        Set MyHybridBody = MySelection.Item(1).Value
        
        Dim vFilter2(1)
        vFilter2(0) = "PlanarFace"
        vFilter2(1) = "HybridShapePlaneExplicit"
    
        MySelection.Clear
        Status = MySelection.SelectElement2(vFilter2, "Select Sketch Plane", True)
        
        If Status = "Normal" Then
            
            Dim Reference1 As Reference
            Set Reference1 = MySelection.Item(1).Value
            
            Dim MySketch 'As Sketch
            Dim i As Integer
            Dim theta As Double
            Dim x As Double, y As Double
            
            For i = 1 To 100
            
                Set MySketch = MyHybridBody.HybridSketches.Add(Reference1)
               
                Dim MyFactory2d As Factory2D
                Set MyFactory2d = MySketch.OpenEdition
                
                If i > 0 And i < 27 Then
                    theta = 90 - 3.6 * (i - 1)
                    theta = theta * 22# / (7# * 180#)
                End If
                
                If i > 26 And i < 52 Then
                    theta = 0 - 3.6 * (i - 26)
                    theta = theta * 22# / (7# * 180#)
                End If
                
                If i > 51 And i < 77 Then
                    theta = -90 - 3.6 * (i - 51)
                    theta = theta * 22# / (7# * 180#)
                End If
                
                If i > 76 And i < 101 Then
                    theta = 180 - 3.6 * (i - 76)
                    theta = theta * 22# / (7# * 180#)
                End If
                
                x = 100# * Cos(theta)
                y = 100# * Sin(theta)
                
                Dim MyLine As Line2D
                Set MyLine = MyFactory2d.CreateLine(0#, 0#, x, y)
                
                Debug.Print MyLine.Name
                
                MySketch.CloseEdition
    
                MyPart.Update
                
                MyPart.InWorkObject = MyHybridBody
                
                Dim hybridShapeFactory1 As Factory
                Set hybridShapeFactory1 = MyPart.HybridShapeFactory
                
                Dim Reference2 As Reference
                Set Reference2 = MyPart.CreateReferenceFromObject(MySketch)
                
                Dim LineDatum
                Set LineDatum = hybridShapeFactory1.AddNewLineDatum(Reference2)
                
                MyHybridBody.AppendHybridShape LineDatum
                
                MyPart.InWorkObject = LineDatum
    
                hybridShapeFactory1.DeleteObjectForDatum Reference2
                
            Next
            
            MyPart.Update

        End If
        
    End If

End Sub
 
Replies continue below

Recommended for you

Code:
Option Explicit
Sub CATMain()

    Dim MyPartDoc As PartDocument
    Dim MyPart As Part
    Dim MySelection 'As Selection    
    Set MyPartDoc = CATIA.ActiveDocument
    Set MyPart = MyPartDoc.Part
    Set MySelection = MyPartDoc.Selection
    
    Dim vFilter1(0)
    Dim Status As String
    
    vFilter1(0) = "HybridBody"
    
    MySelection.Clear
    Status = MySelection.SelectElement2(vFilter1, "Select Geometric Set", True)
    
    If Status = "Normal" Then
    
        Dim MyHybridBody As HybridBody
        Set MyHybridBody = MySelection.Item(1).Value
        
        Dim vFilter2(1)
        vFilter2(0) = "PlanarFace"
        vFilter2(1) = "HybridShapePlaneExplicit"
    
        MySelection.Clear
        Status = MySelection.SelectElement2(vFilter2, "Select Sketch Plane", True)
        
        If Status = "Normal" Then
            
            Dim Reference1 As Reference
            Set Reference1 = MySelection.Item(1).Value
            
            Dim MySketch 'As Sketch
            Dim i As Integer
            Dim theta As Double
            Dim x As Double, y As Double
            
            For i = 1 To 100
            
                Set MySketch = MyHybridBody.HybridSketches.Add(Reference1)
               
                Dim MyFactory2d As Factory2D
                Set MyFactory2d = MySketch.OpenEdition
                Dim PI
               PI = 3.1415926535897932384626433832795   '' all depends on precision :-)
              If i > 0 And i < 27 Then 
              theta = 90 - 3.6 * (i - 1)
               theta= theta * PI/180
               End If
                
                If i > 0 And i < 27 Then
                    theta = 90 - 3.6 * (i - 1)
                    theta= theta * PI/180
                End If
                
                If i > 26 And i < 52 Then
                    theta = 0 - 3.6 * (i - 26)
                   theta= theta * PI/180
                End If
                
                If i > 51 And i < 77 Then
                    theta = -90 - 3.6 * (i - 51)
                    theta= theta * PI/180
                End If
                
                If i > 76 And i < 101 Then
                    theta = 180 - 3.6 * (i - 76)
                    theta= theta * PI/180
                End If
                
                x = 100 * Cos(theta)
                y = 100 * Sin(theta)
                
                Dim MyLine As Line2D
                Set MyLine = MyFactory2d.CreateLine(0, 0, x, y)
                
                '~ Debug.Print MyLine.Name
                
                MySketch.CloseEdition
    
                MyPart.Update
                
                MyPart.InWorkObject = MyHybridBody
                
                Dim hybridShapeFactory1 As Factory
                Set hybridShapeFactory1 = MyPart.HybridShapeFactory
                
                Dim Reference2 As Reference
                Set Reference2 = MyPart.CreateReferenceFromObject(MySketch)
                
                Dim LineDatum
                Set LineDatum = hybridShapeFactory1.AddNewLineDatum(Reference2)
                
                MyHybridBody.AppendHybridShape LineDatum                
                MyPart.InWorkObject = LineDatum    
                hybridShapeFactory1.DeleteObjectForDatum Reference2
                
            Next
            
            MyPart.Update

        End If
        
    End If

End Sub

Regards
Fernando

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top