Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Exporting Catia drafting dimensions to excel

Status
Not open for further replies.

perzentkar

Mechanical
Jun 1, 2018
12
thread560-345707

Good morning all, I'm happy to be a part of the forums.

I am new to using macros in Catia, we are using Catia V5 R19. I am trying to write a macro that will export selected Catia dimensions, tolerances, associated views, and their balloons to a .csv file. I have gotten it to do most of what I want with the help of everyone in the referenced thread. I read the previous thread that's referenced above but I personally have no clue how or where to integrate the Balloon feature that is referenced. I have attached a screenshot of what I would like the end result to look like. The (1) represents the balloon value being displayed prior to the dimensional value. If possible I would like it to be displayed per my screenshot, if that's not possible having it displayed in Column E is more than acceptable. The other tricky part is I would like the Radius, ø, or degree symbol to translate if it applies, So in that instance I would like the value to be displayed like my screenshot ultimately, but even having the symbols display in another column is good because I can write formulas within excel to combine these things. Any help anyone can give me is appreciated.
 
 https://files.engineering.com/getfile.aspx?folder=d647735f-bcfa-4016-84f0-110f71ae68f9&file=screenshot_2.png
Replies continue below

Recommended for you

Hi.

Please, attach a screenshot of a balloon that you want to export.

If possible, attach your CATDrawing so I could take a look at it on Monday and provide working script code.
 
Good morning,

I have created a simple part drawing that serves as a good sample part which includes everything I need. I have also included a second excel spread sheet that includes multiple ways of displaying the data. The first group in excel displays the data with the dimensions, balloons, and eng symbol all in one box. If this would be very complicated to accomplish displaying the data similar to the second excel table is also greatly appreciated. I do not know which one is simpler or even doable. I will write excel formulas to compile all the data as needed. Thank you very much for your help.

[URL unfurl="true"]https://res.cloudinary.com/engineering-com/raw/upload/v1528197465/tips/Excel_macro_sheet_mj0lkb.xlsx[/url]
macro_sheet_screenshot_tyio0v.jpg
 
Good morning all,

Does anyone have any pointers on how to accomplish what I am asking for?
Thanks for the help :)
 
Hi,

Here a code i wrote.
It doesn't export the linked view and i can't test it on your Catdrawing ( I thing your CATIA Release is more than R22).

Hope this can help you.
I added " myworksheet.cells(i + 1, 5).Value = MyDimension.Parent.Parent.Name" for the view name.

Code:
' ------------------------------------------------------
' *** Macro d'export des cotations d'un Catdrawing   ***
' ***           vers Excel                           ***
' ***           Langage VBA                          ***
' ***        Marc Litzler Mai 2016                   ***
' ------------------------------------------------------


Sub CATMain()

Dim myDrawing As DrawingDocument

Dim oTolType As Long
Dim oTolName As String
Dim oUpTol As String
Dim oLowTol As String
Dim odUpTol As Double
Dim odLowTol As Double
Dim oDisplayMode As Long

' ------------------------------------------------------
' *** Vérifie si le document actif est un CATDrawing ***
' ------------------------------------------------------

On Error Resume Next
Set myDrawing = CATIA.ActiveDocument
If (Err.Number <> 0) Then
    MsgBox ("Un CATDrawing doit être actif")
    
    Exit Sub
End If
If (InStr(myDrawing.Name, ".CATDrawing")) = 0 Then
    MsgBox ("La fenêtre active doit être un CATDrawing")
        Exit Sub
End If
Err.Clear
On Error GoTo 0

' *** Sélectionne toutes les cotes ***

Dim selection1 As Selection
Set selection1 = myDrawing.Selection
selection1.Clear
selection1.Search "CATDrwSearch.DrwDimension,all"

' *** Lance Excel ***

Dim xl As Object 'Excel.Application
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
If Err <> o Then
    Set xl = CreateObject("Excel.Application")
    xl.Visible = True
End If

Set workbooks = xl.Application.workbooks
Set myworkbook = xl.workbooks.Add
Set myworksheet = xl.ActiveWorkbook.Add
Set myworksheet = xl.Sheets.Add

' *** titre des colonnes d'Excel ***

myworksheet.Range("A1").Value = "Type"
myworksheet.Range("B1").Value = "Cote"
myworksheet.Range("C1").Value = "Tolérance mini"
myworksheet.Range("D1").Value = "Tolérance maxi"

' *** traitement des cotations ***

For i = 1 To selection1.Count
    Set MyDimension = selection1.Item(i).Value
    MyDimensionValue = MyDimension.GetValue.Value
    ' traitement des tolérances
    MyDimension.GetTolerances oTolType, oTolName, oUpTol, oLowTol, odUpTol, odLowTol, oDisplayMode
    myworksheet.cells(i + 1, 2).Value = MyDimensionValue
    If oTolType = 1 Then 'tolérance numérique
        myworksheet.cells(i + 1, 3).Value = odLowTol
        myworksheet.cells(i + 1, 4).Value = odUpTol
    End If
        If oTolType = 2 Then 'tolérance alphanumérique
        myworksheet.cells(i + 1, 3).Value = oLowTol
        myworksheet.cells(i + 1, 4).Value = oUpTol
    End If
    ' traitement des types de cotations
    MyDimType = MyDimension.DimType
    Select Case MyDimType
        Case 5, 6, 7, 8, 17, 19         'cote type rayon
            MyDimTypeTexte = "R"
        Case 9, 10, 11, 12, 13, 18
            MyDimTypeTexte = "Ø"        'cote type diamètre
        Case 14
            MyDimTypeTexte = "Ch"       'cote type chanfrein
        Case 4
            MyDimTypeTexte = "Angle"    'cote d'angle
        Case Else
            MyDimTypeTexte = ""         'cote type longueur-distance
    End Select
    myworksheet.cells(i + 1, 1).Value = MyDimTypeTexte

' to get view name added juin 2018
     myworksheet.cells(i + 1, 5).Value = MyDimension.Parent.Parent.Name
    odLowTol = 0
    odUpTol = 0
    oUpTol = ""
    oLowTol = ""

Next

End Sub

Regards
Marc
 
thank you very much for the help. I'll see if I can combine the two macros in some way and get the associated view to populate.
 
Thank you very much :) This works perfectly for my needs.
 
Good afternoon,

How would I go about getting this macro to export the values in imperial units verses metric? What Value do I need to change? How do I get it to pull the same number of decimals that are displayed in the drawing?
 
Hi perzentkar,

I complete the code with the decimal numbers and unit managing.
Code:
' ----------------------------------------------------------
' *** Macro d'export des cotations d'un Catdrawing       ***
' ***           vers Excel                               ***
' ***           Langage VBA                              ***
' ***        Marc Litzler Mai 2016                       ***
' *** Marc Litzler 07/2018 add decimal and unit managing ***
' ----------------------------------------------------------


Sub CATMain()

Dim myDrawing As DrawingDocument

Dim oTolType As Long
Dim oTolName As String
Dim oUpTol As String
Dim oLowTol As String
Dim odUpTol As Double
Dim odLowTol As Double
Dim oDisplayMode As Long
Dim MyFormatPrecision ' As Double
' ------------------------------------------------------
' *** Vérifie si le document actif est un CATDrawing ***
' ------------------------------------------------------

On Error Resume Next
Set myDrawing = CATIA.ActiveDocument
If (Err.Number <> 0) Then
    MsgBox ("Un CATDrawing doit être actif")
    
    Exit Sub
End If
If (InStr(myDrawing.Name, ".CATDrawing")) = 0 Then
    MsgBox ("La fenêtre active doit être un CATDrawing")
        Exit Sub
End If
Err.Clear
On Error GoTo 0

' *** Sélectionne toutes les cotes ***

Dim selection1 As Selection
Set selection1 = myDrawing.Selection
selection1.Clear
selection1.Search "CATDrwSearch.DrwDimension,all"

' *** Lance Excel ***

Dim xl As Object 'Excel.Application
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
If Err <> o Then
    Set xl = CreateObject("Excel.Application")
    xl.Visible = True
End If

Set Workbooks = xl.Application.Workbooks
Set myworkbook = xl.Workbooks.Add
Set myworksheet = xl.ActiveWorkbook.Add
Set myworksheet = xl.Sheets.Add

' *** titre des colonnes d'Excel ***

myworksheet.Range("A1").Value = "Type"
myworksheet.Range("B1").Value = "Dimension"
myworksheet.Range("C1").Value = "Tol. min"
myworksheet.Range("D1").Value = "Tol. max"
myworksheet.Range("E1").Value = "View name"

' *** traitement des cotations ***

For i = 1 To selection1.Count
    Set MyDimension = selection1.Item(i).Value
    MyDimensionValue = MyDimension.GetValue.Value
    'Get the number of decimals
    MyFormatPrecision = Len(Str(MyDimension.GetValue.GetFormatPrecision(1))) - 2 ' (1: main value; 2: dual value )
    ' Get the dimension unit : =0 if mm / =1 if inch
    MyDimensionUnit = MyDimension.GetValue.GetDisplayUnit(1) ' (1: main value; 2: dual value )
    'Convert the dimension if inch
    If MyDimensionUnit = 1 Then
        myworksheet.Cells(i + 1, 2).Value = Round(MyDimensionValue / 25.4, MyFormatPrecision)
    Else
        myworksheet.Cells(i + 1, 2).Value = Round(MyDimensionValue, MyFormatPrecision)
    End If
     ' traitement des tolérances
    MyDimension.GetTolerances oTolType, oTolName, oUpTol, oLowTol, odUpTol, odLowTol, oDisplayMode
   
    If oTolType = 1 Then 'tolérance numérique
            'Convert the tolerance if inche
        If MyDimensionUnit = 1 Then
            myworksheet.Cells(i + 1, 3).Value = Round(odLowTol / 25.4, MyFormatPrecision)
            myworksheet.Cells(i + 1, 4).Value = Round(odUpTol / 25.4, MyFormatPrecision)
        Else
            myworksheet.Cells(i + 1, 3).Value = odLowTol
            myworksheet.Cells(i + 1, 4).Value = odUpTol
        End If
    End If
        If oTolType = 2 Then 'tolérance alphanumérique
        myworksheet.Cells(i + 1, 3).Value = oLowTol
        myworksheet.Cells(i + 1, 4).Value = oUpTol
    End If
    ' traitement des types de cotations
    MyDimType = MyDimension.DimType
    Select Case MyDimType
        Case 5, 6, 7, 8, 17, 19         'cote type rayon
            MyDimTypeTexte = "R"
        Case 9, 10, 11, 12, 13, 18
            MyDimTypeTexte = "Ø"        'cote type diamètre
        Case 14
            MyDimTypeTexte = "Ch"       'cote type chanfrein
        Case 4
            MyDimTypeTexte = "Angle"    'cote d'angle
        Case Else
            MyDimTypeTexte = ""         'cote type longueur-distance
    End Select
    
    myworksheet.Cells(i + 1, 1).Value = MyDimTypeTexte

' to get view name added juin 2018
     myworksheet.Cells(i + 1, 5).Value = MyDimension.Parent.Parent.Name
    odLowTol = 0
    odUpTol = 0
    oUpTol = ""
    oLowTol = ""

Next

End Sub

Regards
Marc
 
Good morning,

Thank you for all the help. This has been a great tool. How do I get it to export the values into excel in the order in which I select them on my drawing? If I wanted it to export a text such as 1/4-20 tap How would I go about doing that? Or is it just better to manually enter that into excel?
 
Hi perzentkar,

The macro select automatically all drawing dimensions and then they are exported to Excel.
How would you like to proceed? Select each dimension one per one?
Would you export only dimensions or drawing texts too?
Is “1/4-20 tap » a drawing text or a post/prefix from a dimension ?

CATDrawing_v35okg.jpg


Regards
Marc
 
Good morning,

I would establish a tap callout via text with leader. I don't think I would ever have it attached to a dimension. I would like to be able to select all the dimensions manually, then run the macro. I would like the macro to export the dimensions to excel following the order I selected them in. If this is an overly difficult task it's not necessary.
 
Good morning,

I don’t know how to export in the same order the element witch were selected before launching the macro.
So I did it another way : interactively.
-open the Catdrawing
-run the macro --> the macro opens a new Excel Workbook and asks the user to select a dimension or a text ( in the status bar(1))
-each time you select a Drawing Dimension or a Drawing Text(2), it will be exported -->new line in Excel Table(3)
-To stop the macro press ESC taste.

Export_Drawing_to_Excel_baghsh.jpg


Code:
' ----------------------------------------------------------
' *** Macro d'export des cotations d'un Catdrawing       ***
' ***           vers Excel                               ***
' ***           Langage VBA                              ***
' ***        Marc Litzler Mai 2016                       ***
' *** Marc Litzler 07/2018 add decimal and unit managing ***
' ----------------------------------------------------------


Sub CATMain()

Dim myDrawing As DrawingDocument
Dim MyObject As Object
Dim oTolType As Long
Dim oTolName As String
Dim oUpTol As String
Dim oLowTol As String
Dim odUpTol As Double
Dim odLowTol As Double
Dim oDisplayMode As Long
Dim MyFormatPrecision ' As Double
' ------------------------------------------------------
' *** Check if a CATDrawing is active                ***
' ------------------------------------------------------

On Error Resume Next
Set myDrawing = CATIA.ActiveDocument
If (Err.Number <> 0) Then
    MsgBox ("Un CATDrawing doit être actif")
    
    Exit Sub
End If
If (InStr(myDrawing.Name, ".CATDrawing")) = 0 Then
    MsgBox ("La fenêtre active doit être un CATDrawing")
        Exit Sub
End If
Err.Clear
On Error GoTo 0

Dim selection1 As Selection
Set selection1 = myDrawing.Selection
selection1.Clear

' *** Launch Excel ***

Dim xl As Object 'Excel.Application
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
If Err <> o Then
    Set xl = CreateObject("Excel.Application")
    xl.Visible = True
End If

Set Workbooks = xl.Application.Workbooks
Set myworkbook = xl.Workbooks.Add
Set myworksheet = xl.ActiveWorkbook.Add
Set myworksheet = xl.Sheets.Add

' *** title of Excel columns ***

myworksheet.Range("A1").Value = "Type"
myworksheet.Range("B1").Value = "Dimension"
myworksheet.Range("C1").Value = "Tol. min"
myworksheet.Range("D1").Value = "Tol. max"
myworksheet.Range("E1").Value = "View name"

' *** Loop for selection and export until ESC is pusch on ***
i = 1
Do
 Set mySelectObj = selection1
    Dim InputObjectType(1)
    InputObjectType(1) = "DrawingText"  '"AnyObject"
    InputObjectType(0) = "DrawingDimension"
    Status = mySelectObj.SelectElement2(InputObjectType(), "Select a Text or a Dimension / Press ESC to stop", False)
    If (Status = "Cancel") Then
        MsgBox "Macro was stopped by ESC"
        Exit Sub
    End If

Set MyObject = mySelectObj.Item(1)

If MyObject.Type = "DrawingDimension" Then ' if a Drawing Dimension is selected
    Set MyDimension = mySelectObj.Item(1).Value 'selection1.Item(i).Value
    MyDimensionValue = MyDimension.GetValue.Value
    'Get the number of decimals
    MyFormatPrecision = Len(Str(MyDimension.GetValue.GetFormatPrecision(1))) - 2 ' (1: main value; 2: dual value )
    ' Get the dimension unit : =0 if mm / =1 if inch
    MyDimensionUnit = MyDimension.GetValue.GetDisplayUnit(1) ' (1: main value; 2: dual value )
    'Convert the dimension if inch
    If MyDimensionUnit = 1 Then
        myworksheet.Cells(i + 1, 2).Value = Round(MyDimensionValue / 25.4, MyFormatPrecision)
    Else
        myworksheet.Cells(i + 1, 2).Value = Round(MyDimensionValue, MyFormatPrecision)
    End If
     ' managing tolerances
    MyDimension.GetTolerances oTolType, oTolName, oUpTol, oLowTol, odUpTol, odLowTol, oDisplayMode
   
    If oTolType = 1 Then ' numeric tolerances
            'Convert the tolerance if inche
        If MyDimensionUnit = 1 Then
            myworksheet.Cells(i + 1, 3).Value = Round(odLowTol / 25.4, MyFormatPrecision)
            myworksheet.Cells(i + 1, 4).Value = Round(odUpTol / 25.4, MyFormatPrecision)
        Else
            myworksheet.Cells(i + 1, 3).Value = odLowTol
            myworksheet.Cells(i + 1, 4).Value = odUpTol
        End If
    End If
        If oTolType = 2 Then ' alphanumeric tolerances
        myworksheet.Cells(i + 1, 3).Value = oLowTol
        myworksheet.Cells(i + 1, 4).Value = oUpTol
    End If
    ' managing dimensions type
    MyDimType = MyDimension.DimType
    Select Case MyDimType
        Case 5, 6, 7, 8, 17, 19             'radius type rayon
            MyDimTypeTexte = "R"
        Case 9, 10, 11, 12, 13, 18
            MyDimTypeTexte = "Dia"         'diameter type
        Case 14
            MyDimTypeTexte = "Ch"           'chamfer type
        Case 4
            MyDimTypeTexte = "Angle"        'angle type
        Case Else
            MyDimTypeTexte = "Length"       'lenght type
    End Select
    
    myworksheet.Cells(i + 1, 1).Value = MyDimTypeTexte

    odLowTol = 0
    odUpTol = 0
    oUpTol = ""
    oLowTol = ""


Else '  if a Drawing Text is selected
    myworksheet.Cells(i + 1, 1).Value = "Text"
    myworksheet.Cells(i + 1, 2).Value = MyObject.Value.Text
End If
myworksheet.Cells(i + 1, 5).Value = MyObject.Value.Parent.Parent.Name
i = i + 1
Loop
End Sub

Regards
Marc
 
Marc, instead of SelectElement2 use the SelectElement3 method, it allows multiple selection.

regards,
LWolf
 
Hello.
I am looking for a macro which export only angle value to excel.
I made it but it converts the degree to radian in excel.
 
Hi

@LWolf:
This is a good tip I will try it.

@ashish2112:
The angle unit for CATIA is radian that why the macro export radian.
You have to convert in degree:
PI = 4 * Atn(1)
myworksheet.Cells(i + 1, 2).Value = (MyDimensionValue * 180 / PI)



Regards
Marc
 
Hello Mr. Marc
Thanks for your reply.
I worked with your solution. but it converts the all the dimension(linear/angular/diametrical/radius dimension).
Basically I am exporting my dimension to excel.
linear dimension is exactly exported to excel but for the angular it is converting it to radian. I need to export angle in degree.

Regards
Ashish Kumar
 
Hello Ashish,

You should write the conversion only for angle, here in the code:

Select Case MyDimType
Case 5, 6, 7, 8, 17, 19 'radius type rayon
MyDimTypeTexte = "R"
Case 9, 10, 11, 12, 13, 18
MyDimTypeTexte = "Dia" 'diameter type
Case 14
MyDimTypeTexte = "Ch" 'chamfer type
Case 4
MyDimTypeTexte = "Angle" 'angle type
[highlight #FCE94F]myworksheet.Cells(i + 1, 2).Value = (MyDimensionValue * 180 / PI)
[/highlight]
Case Else
MyDimTypeTexte = "Length" 'lenght type
End Select


Regards
Marc
 
Good morning Guys,
The macro is working perfectly and exports everything as desired automatically. We have elected to just manually enter tap callouts and such. I see that in the macro it opens excel and exports the values to a new document. How would I go about making it export to a specific excel document and loading the values accordingly? I want to have it automatically export the values into a template.
 
Hi,

Instead of
Code:
Set xl = GetObject(, "Excel.Application")
If Err <> o Then
    Set xl = CreateObject("Excel.Application")
    xl.Visible = True
End If

Set Workbooks = xl.Application.Workbooks
Set myworkbook = xl.Workbooks.Add
Set myworksheet = xl.ActiveWorkbook.Add
Set myworksheet = xl.Sheets.Add
write:
Code:
Set xl = CreateObject("Excel.Application")
Set Workbooks = xl.Application.Workbooks
Set myworkbook = Workbooks.Open(ExcelTemplate) 'ExcelTemplate = diretory/name of Excel Template
Set myworksheet = myworkbook.Worksheets.Item(1) 'Item(x) or Item("name of sheet")

Regards
Marc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor