Hide Table of Contents

Export File as Specified File Type Example (COM native C++)

This code snippet shows how to export a file to a specified file type.

 

//Get IDocumentExporter object
    IDocumentExporterPtr dsDocExporter = dsDoc->GetDocumentExporter();
    
if( NULL == dsDocExporter )
    {
        ::AfxMessageBox( L
"Failed to get IDocumentExporter object of the current document." );

        
return;
    }

    
//Display SaveAs dialog to export file to the selected format
    CString filesFilter = L"Bitmap (*.bmp)|*.bmp|Windows Enhanced Metafile (*.emf)|*.emf|JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png|Slide (*.sld)|*.sld|Scalable Vector Graphics Format (*.svg)|*.svg|Tag Image Format (*.tif)|*.tif|Stereolithography (*.stl)|*.stl||";
    CStringArray strExtList;
    strExtList.Add( L
".bmp" );
    strExtList.Add( L
".emf" );
    strExtList.Add( L
".jpg" );
    strExtList.Add( L
".png" );
    strExtList.Add( L
".sld" );
    strExtList.Add( L
".svg" );
    strExtList.Add( L
".tif" );
    strExtList.Add( L
".stl" );

    CFileDialog fileSaveDialog ( FALSE, NULL, L
"ExportedFile", OFN_OVERWRITEPROMPT, filesFilter );
    
    INT_PTR result = fileSaveDialog.DoModal();
    
if( IDOK == result )
    {
        DWORD nFilterIndex = fileSaveDialog.m_ofn.nFilterIndex;
        CString fullPathName = fileSaveDialog.GetPathName() + strExtList[nFilterIndex -1];
        bstr_t expFileName ( fullPathName );

        
//Get export type from file name extension
        ExportType exportType = GetExportTypeFromFileExt( GetFileExtension( fullPathName ) );
        VARIANT_BOOL bSuccess = VARIANT_FALSE;

        
switch (exportType)
        {
        
case eBMP:
            {
                
//Export to BMP image
                dsDocExporter->ExportToBmp( fullPathName.AllocSysString(), &bSuccess );
                
if( VARIANT_FALSE == bSuccess )
                {
                    ::AfxMessageBox( L
"Failed to export current document to BMP image." );
                }

                
break;
            }
        
case eEMF:
            {
                
//Export to EMF format
                dsDocExporter->ExportToEmf( fullPathName.AllocSysString(), &bSuccess );
                
if( VARIANT_FALSE == bSuccess )
                {
                    ::AfxMessageBox( L
"Failed to export current document to EMF format." );
                }

                
break;
            }
        
case eJPG:
            {
                
//Export to JPG image
                dsDocExporter->ExportToJpg( fullPathName.AllocSysString(), &bSuccess );
                
if( VARIANT_FALSE == bSuccess )
                {
                    ::AfxMessageBox( L
"Failed to export current document to JPG image." );
                }

                
break;
            }
        
case ePNG:
            {
                
//Export to PNG image
                dsDocExporter->ExportToPng( fullPathName.AllocSysString(), &bSuccess );
                
if( VARIANT_FALSE == bSuccess )
                {
                    ::AfxMessageBox( L
"Failed to export current document to PNG image." );
                }

                
break;
            }
        
case eSLD:
            {
                
//Export to SLD format
                dsDocExporter->ExportToSld( fullPathName.AllocSysString(), &bSuccess );
                
if( VARIANT_FALSE == bSuccess )
                {
                    ::AfxMessageBox( L
"Failed to export current document to SLD format." );
                }

                
break;
            }
        
case eSVG:
            {
                
//Export to SVG format
                dsDocExporter->ExportToSvg( fullPathName.AllocSysString(), &bSuccess );
                
if( VARIANT_FALSE == bSuccess )
                {
                    ::AfxMessageBox( L
"Failed to export current document to SVG format." );
                }

                
break;
            }
        
case eTIFF:
            {
                
//Export to TIFF image
                dsDocExporter->ExportToTiff( fullPathName.AllocSysString(), &bSuccess );
                
if( VARIANT_FALSE == bSuccess )
                {
                    ::AfxMessageBox( L
"Failed to export current document to TIFF image." );
                }

                
break;
            }
        
case eSTL:
            {
                
//Export to STL format
                dsDocExporter->ExportToStl( fullPathName.AllocSysString(), &bSuccess );
                
if( VARIANT_FALSE == bSuccess )
                {
                    ::AfxMessageBox( L
"Failed to export current document to STL format." );
                }

                
break;
            }
        }
    }
}

//Method to get export type from file name extension
ExportType CExportToCommandRunner::GetExportTypeFromFileExt( CString fileExtension )
{
    ExportType exportType = eUndefinedExpType;

    
if( fileExtension.MakeUpper() == "BMP" )
    {
        exportType = eBMP;
    }

    
if( fileExtension.MakeUpper() == "EMF" )
    {
        exportType = eEMF;
    }

    
if( fileExtension.MakeUpper() == "JPG" )
    {
        exportType = eJPG;
    }

    
if( fileExtension.MakeUpper() == "PNG" )
    {
        exportType = ePNG;
    }

    
if( fileExtension.MakeUpper() == "SLD" )
    {
        exportType = eSLD;
    }

    
if( fileExtension.MakeUpper() == "SVG" )
    {
        exportType = eSVG;
    }

    
if( fileExtension.MakeUpper() == "TIF" )
    {
        exportType = eTIFF;
    }

    
if( fileExtension.MakeUpper() == "STL" )
    {
        exportType = eSTL;
    }

    
return exportType;
}

CString CExportToCommandRunner::GetFileExtension(
const CString &fullPath )
{
    CString fileExtension = L
"";    
    
int position;

    
if( (position = fullPath.ReverseFind( '.' ) ) != -1 )
    {
        fileExtension = fullPath.Right( fullPath.GetLength() - position - 1 );
    }

    
return fileExtension;
}

 



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Export File as Specified File Type Example (COM native C++)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2025 SP2

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.