catcom

JOB 2016-07-06, The following package provides a Catia/COM interface based on TWAPI.

The current implementation supports to open / close a CAD model from within a Tcl program. There is also a function available, which allows to export native data to a different file format (STEP and PDF respectively). In fact, the CAD Program can be driven via the COM interface in a similar manner than any other office application (like Excel or Word, etc. ...).

Once the COM object is initialized via twapi, one might use the -print option to investigate the available sub-functions any further.

# catcom.tcl
# -------------------------------------------------------------------------
# (c) 2016, Johann Oberdorfer - Engineering Support | CAD | Software
#     johann.oberdorfer [at] gmail.com
#     www.johann-oberdorfer.eu
# -------------------------------------------------------------------------
# This source file is distributed under the BSD license.
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#   See the BSD License for more details.
# -------------------------------------------------------------------------

# Purpose:
#  Package to establish Catia/COM interface based on twapi.
#
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Revision history:
#   07.06.16: J.Oberdorfer, initial release
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
 
package require twapi
package provide catcom 0.1


namespace eval catcom {

        namespace export getCatiaApp
        namespace export catia.Documents.Open
        namespace export catia.ActiveDocument.Close
        namespace export catia.ActiveDocument.SaveAs 

        # attempt to get connection to a running Catia session via tcom

        proc getCatiaApp {catapp} {
                upvar $catapp app
                return [catch {[set app [twapi::comobj "Catia.Application" -active]]} errmsg]
        }

        
        # CATIA.Documents.Open(<file_name>)
        # nativename is mandatory here to ensure COM object get's a valid filename

        proc catia.Documents.Open {catapp cad_model} {

                set cat [$catapp Application]
                set docs [$cat Documents]

                # make sure to supress nasty warning dialogs
                # may be this can be set system wide in the startup configuration setting as well?!

                $cat -set DisplayFileAlerts 0
                $cat -set RefreshDisplay 1

                $docs Open [file nativename $cad_model]
        }

        proc catia.ActiveDocument.Close {catapp} {

                set active_doc [$catapp ActiveDocument]
                $active_doc Close
        }


        # valid fformat arguments: "stp", "pdf"
        #   (name must match Catia's ExportData requirements!)
        # 

        proc catia.ActiveDocument.SaveAs {catapp export_dir cad_model {fformat ""}} {
                global env

                # convert to lower case (maybe not really required...)
                set fformat [string tolower $fformat]

                switch -- $fformat {
                        "stp"  { set ext ".stp" }
                        "pdf"  { set ext ".pdf" }
                        default {
                                tk_messageBox \
                                        -title "Programmer's error" \
                                        -icon "error" \
                                        -message "Wrong argument given to function: CATIA.ActiveDocument.SaveAs" \
                                        -type ok
                                exit 0
                        }
                }

                # a.) change file extension:
                set file_name [file tail [file rootname $cad_model]]
                append file_name $ext
        
                set export_file_name \
                                [file nativename [file join $env(TEMP) $file_name]]
                set new_file_name \
                                [file nativename [file join $export_dir $file_name]]

                # b.) delete all models previously created (if any)...
                file delete -force $export_file_name
                file delete -force $new_file_name

                set active_doc [$catapp ActiveDocument]

                # c.) export to local TEMP directory 1st -> hint: export runs
                #     magnitudes faster (maybe due to virus protection check
                #     on the remote file server ?)

                $catapp -set StatusBar "Executing ExportData: $export_file_name $fformat"
                $active_doc ExportData $export_file_name $fformat
        
                # d.) and finally - move the file back to the data server:
                file copy -force $export_file_name $new_file_name
                file delete -force $export_file_name
        }
}

Package might be used e.g. such as:

# catcom_test.tcl

# where to find twapi
lappend auto_path [file join [file dirname [info script]]]
lappend auto_path [file join [file dirname [info script]] "../lib"]


package require Tk
package require twapi
package require catcom 0.1
        namespace import catcom::*


# some test models...

set cad_model "C:/yourProject/Test.CATPart"
set cad_model1 "C:/yourProjekte/Test.CATDrawing"
set export_dir "C:/yourExportDir"


# here we go ...
catch {console show}


if { ![getCatiaApp catapp] } {

        wm withdraw .
        set msg "Communication to Catia not possible!\n"
        append msg "Start Catia and try again!"
        
        tk_messageBox \
                -title "Catia COM Interface Error" \
                -icon "error" \
                -message $msg \
                -type ok

        exit 0
}


$catapp -set Visible 1
$catapp -set StatusBar "Hello World."

# usefull to print all available commands of the available COM interface
# $catapp -print

# STEP Export
$catapp -set StatusBar "Exporting current CATPart to STEP file..."
catia.Documents.Open $catapp $cad_model
catia.ActiveDocument.SaveAs $catapp $export_dir $cad_model "stp"
catia.ActiveDocument.Close $catapp

# PDF Export
$catapp -set StatusBar "Exporting current CATDrawing to PDF file..."
catia.Documents.Open $catapp $cad_model1
catia.ActiveDocument.SaveAs $catapp $export_dir $cad_model1 "PDF"
catia.ActiveDocument.Close $catapp

APN TWAPI's COM does not do garbage collection so I suspect the above code would leak memory / objects. For example, cat and docs in the Open proc need to be destroyed by calling their destroy or -destroy methods.