Mechanical Design

2D Layout for 3D Design

Print 2D Layout by Generating a Drawing Document

How to create 2DL Views from View or Capture created in 3D Functional Tolerancing & Annotation Application

Use Case

Abstract

    This article explains how to print a 2D Layout by creating previously a drawing. the print is executed by a batch launched by a CAA command accessible in the 2D Layout workbench. By launching a batch the CATIA is not frozen during the treatment.

Note: the use case is only available on Windows Operating System but there is no block point to create an equivalent command on Unix Operating System.

    2DL means: 2D Layout for 3D Design, this is the name of the CATIA application.


What You Will Learn With This Use Case

This use case is intended to show:

[Top]

The Architecture of the Use Case

This use case is shared on several modules, all these modules are defined in the CAADrafting2DLInterfaces.edu framework that illustrates Drafting2DLInterfaces framework capabilities.

 

Fig. 1: Overview of the use case

 

 

[Top]

What Does CAA2DLAddin Do?

This module contains the code to add a CAA command in the 2DL workbench by using the CATI2DLMainWkbAddin interface defined in Drafting2DLInterface framework.

 

Fig. 2: the 2DL workbench with the CAA toolbar containing the "print To Batch" command

 

Top]

What Does CAA2DPrintToDrafting Do?

This module contains the code of the command. This command has only one step implemented in the Activate method of the CATCommand. The command save the current CATPart document in the temporary file by using SaveAsNew method defined in CATDocumentServices class, then the CAA Batch is launched. When the batch is ended a popup window is displayed to inform the user the document is printed.

 

Fig. 3: The display windows

The first window is displayed when the batch is correctly ended. The second window is displayed when printer environment variables are not set.

Top]

What Does CAA2DConvertToDraw Do?

This module contains the code of the batch launched by the command. This batch is based on CAA batch infrastructure [1]. The batch opens the Part document saved in temporary directory by the command, retrieves the 2DL Layout root, extract a drawing document from the 2D Layout root, creates a print file from the drawing and launches the print file to the printer. The details of the batch is explained in the topic Step-by-step

Top]

How to Launch the print 2DL Command

To launch print 2DL command, you will need to set up the build time environment, then compile CAA2DLAddin, CAA2DLConvertToDraw.m and CAA2DLPrintToDrafting.m modules along with its prerequisites, set up the run time environment, and then start CATIA.

Do not forget to set printer name and printer server name before launching CATIA

Set CAT_CAA2DLPrinterName=MyPrinterName
Set CAT_CAA2DLPrinterServerName=MyPrinterServerName
 

When you launch CATIA you can open the Part document delivered in the following path: CAADrafting2DLInterfaces.edu\CNext\resources\graphic\CAA2DLPrintToBatch.CATPart.

[Top]

Where to Find the CAA2DLCreate2DLViewFromFTA  Code

The use case is made of a several module named CAA2DLPrintToBatch, CAA2DLAddin and CAA2DLConvertToDraw located in the CAADrafting2DLInterfaces.edu framework:

Windows InstallRootDirectory\CAADrafting2DLInterfaces.edu

where InstallRootDirectory is the directory where the CAA CD-ROM is installed.

[Top]

Step-by-Step

Batch launching:

  1. Launching the batch

Batch execution:

  1. Retrieving the arguments from the XML file
  2. Retrieving the 2D Layout Root from the Part Document
  3. Retrieving the 2D Layout Standard and Format of the Active Sheet
  4. Creating and Initializing the new Drawing Document
  5. Printing the Drawing Document by using the EPS print format
  6. Launching the print file to the printer
  7. Removing Document and Exiting

[Top]

Launching the batch

 //Here we generate the XML parameter file corresponding to the batch: 
CATBatchParameters input_param;
input_param.BeginBuffer("batchConvertToDraw", "ParamTest.xml"); 
...

A ParamTest.xml file containing all information for the batch execution is created in the temporary file. batchConvertToDraw descriptor is the delivered in the following path: CAADrafting2DLInterfaces.edu/CNext/resources/batchdesc/batchConvertToDraw.xml

 

Fig. 4: The batchConvertToDraw.xml file

This XML file contains the name of the batch and the dll. there is also information as catutil="0" to specify the batch is not accessible accross catutil tool.

 

input_param.BeginInput();
input_param.InsertFile("file1",storageNameDocument, "", COMM_FILE_BINARY);

// The batch works with the parent standard of standard used by the 2D Layout
// To work with a specific standard replace ParentStd by the DRAWING standard name.
// Drawing standard and 2D Layout standard must be compatible:
// - Identical parent standards
// - Identical dimension formats: (Tolerance and Value)

// Drawing standard name (Optional)
const char* cDrawingStdName = CATGetEnv("CAT_CAA2DLDrawingStandardName");
if (cDrawingStdName)
{
CATUnicodeString sPrinterSrvName (cDrawingStdName);
input_param.InsertSimpleArg("drwStandard", sPrinterSrvName);
}
// If the environment variable is not set. The Drawing will be created with the parent standard of the 2DLayout Standard
else
input_param.InsertSimpleArg("drwStandard", "ParentStd");

// Sheet Print Mode (Optional)
const char* cSheetPrintMode = CATGetEnv("CAT_CAA2DLSheetPrintMode");
if (cSheetPrintMode)
{
CATUnicodeString sSheetPrintMode (cSheetPrintMode);
input_param.InsertSimpleArg("SheetPrintMode", sSheetPrintMode);
}
// If the environment variable is not set. Only ActiveSheet is printed
else
input_param.InsertSimpleArg("SheetPrintMode", "ActiveSheet");

// Printer Name initialization
input_param.InsertSimpleArg("PrinterName", sPrinterName);

// Printer Server name initialization
input_param.InsertSimpleArg("PrinterServerName", sPrinterSrvName);
input_param.EndInput();

// Here we describe the output directory for BatchSample.
const char* batch_home = CATGetEnv("BATCH_HOME");
const char* out_folder = batch_home; // we choose to write the outputs in $BATCH_HOME. 
input_param.BeginOutput();
input_param.EndOutput();
input_param.EndBuffer();
...

This section shows how to save input arguments for the batch in a XML file.

 

//Retrieve the ClientMonitor, main interface to start and monitor batches
CATBatClientMonitorCAA* cliMonitor = CATBatClientMonitorCAA::GetTheClientMonitorCAA();
if (!cliMonitor) return CATStatusChangeRCAborted;

//Monitor V5 batches initialization
int batchMode = 0;
HRESULT hr = cliMonitor->InitializeV5Monitoring(batchMode);

...

This section launches the batch.

[Top]

Retrieving the arguments from the XML file

...
int batchmain (int argc, char** argv)
{

  int Error_code = 0;

  // 0/ CREATE THE SESSION AND READ THE INFORMATION IN THE XML FILE ASSOCIATED TO THE BATCH
  // ======================================================================================
  CATSession *pSampleSession = NULL;
  HRESULT hr = ::Create_Session("SampleSession",pSampleSession);
  if (FAILED(hr)) return 60;

  //Retrieve the CATBatStatePub object used to publish a completion state for the batch
  CATBatStatePubCAA* pub = CATBatStatePubCAA::GetCAAStatePub();
  if (!pub) return 61;

  //Retrieve CATIBatch the main interface to access XML parameter file from the batch 
  CATIBatchCAA* batch = GetCATIBatchCAA();
  if (!batch) return 62;

  //Retrieves InputParameters section from the XML parameter file
  CATIBatchElementCAA* inputParameters = NULL;
  hr = batch->get_InputParametersCAA(inputParameters);
  if ( FAILED(hr) || !inputParameters )
  return 63;

  // Standard Name
  CATIBatchElementCAA* oDrwStd = NULL;
  CATUnicodeString usId = "drwStandard";
  CATUnicodeString stdName;
  if (SUCCEEDED(inputParameters->ParameterCAA(usId, oDrwStd)) && oDrwStd)
  {
    hr = oDrwStd->StringArg(stdName);
    oDrwStd->Release();oDrwStd = NULL;
    if ( FAILED(hr) ) return 64;
  }

  // Printer name
  CATIBatchElementCAA* oPrtName = NULL;
  usId = "PrinterName";
  CATUnicodeString sPrinterName;
  if (SUCCEEDED(inputParameters->ParameterCAA(usId, oPrtName)) && oPrtName)
  {
    hr = oPrtName->StringArg(sPrinterName);
    oPrtName->Release();oPrtName = NULL;
    if ( FAILED(hr) ) return 65;
  }

  // Printer server name
  CATIBatchElementCAA* oPrtSrvName = NULL;
  usId = "PrinterServerName";
  CATUnicodeString sPrinterSrvName;
  if (SUCCEEDED(inputParameters->ParameterCAA(usId, oPrtSrvName)) && oPrtSrvName)
  {
    hr = oPrtSrvName->StringArg(sPrinterSrvName);
    oPrtSrvName->Release();oPrtSrvName = NULL;
    if ( FAILED(hr) ) return 66;
  }

  // Sheet Print Mode
  CATIBatchElementCAA* oSheetPrt = NULL;
  usId = "SheetPrintMode";
  CATUnicodeString sSheetPrintMode;
  if (SUCCEEDED(inputParameters->ParameterCAA(usId, oSheetPrt)) && oSheetPrt)
  {
    hr = oSheetPrt->StringArg(sSheetPrintMode);
    oSheetPrt->Release();oSheetPrt = NULL;
    if ( FAILED(hr) ) return 66;
  }

  //Retrieves all the <file> tags included in the inputParameters section
  // If the batch is launched from the command PrintToDraftingCmd, there is only one occurence.
  CATIBatchElementsCAA* inputFiles = NULL;
  hr = inputParameters->get_FileListCAA(inputFiles);
  if ( FAILED(hr) || !inputFiles )
    return 67;

  // Scans the list of <file> 
  long childrenCount = 0;
  hr = inputFiles->get_Count(childrenCount);
  CATIBatchElementCAA * elem = NULL;

  for ( int i = 0; i<childrenCount; i++ )
  {
    hr = inputFiles->ItemCAA( i, elem );
    if ( hr != S_OK )
    return 68;
    // for each <file> retrieve the file path corresponding to the attribute "filePath" of the 
    // <file> tag.
    CATUnicodeString usfile_path;
    elem->get_Path(usfile_path); 

    elem->Release();
    elem = NULL;
    // Writes this path in the log of the batch available in $BATCH_HOME/uuid/uuidLog.txt 
    // at the end of the execution. Default for BATCH_HOME is /tmp or C:\temp and uuid is a
    // generated identifier for each batch execution.

    // Checks the file existence and the right to read it.
    if (CATFileAccess(usfile_path.CastToCharPtr() , 04) != CATLibSuccess)
      return 69;
  }

  // Retrieves the directory name in the usfile_path path
  char in_filename[CATMaxPathSize];
  char in_dirname[CATMaxPathSize];
  memset(in_filename, '\0', CATMaxPathSize);
  memset(in_dirname, '\0', CATMaxPathSize);
  CATSplitPath( usfile_path.CastToCharPtr(), in_dirname, in_filename);
  CATUnicodeString dirToWork = in_dirname;
  CATUnicodeString partName = in_filename;
...

The section describes the code to read input arguments store in the XML file.

[Top]

 

Retrieving the 2D Layout Root from the Part Document

...
// 1/ Opens the Part document containing the 2DLayout to convert
// ============================================================
CATDocument *pDocPart = NULL;
CATInit_var spInitOnDoc;
if( SUCCEEDED(CATDocumentServices::OpenDocument(usfile_path, pDocPart)) && pDocPart)
{
  // To Prevent the destruction
  CATLockDocument(*pDocPart);
  spInitOnDoc = pDocPart;
}

// 2/ Retrieves the Mechanical Part
// ================================
CATIPrtPart_var spPart;
CATIPrtContainer * piPrtCont = (CATIPrtContainer*) spInitOnDoc->GetRootContainer("CATIPrtContainer"); 
if (piPrtCont)
{
  // Gets the part feature of the container.
  spPart = piPrtCont->GetPart();
  if (NULL_var != spPart)
  else
  {
    Error_code = 70;
  }
  piPrtCont->Release();piPrtCont=NULL;
}

// 3/ Gets the 2DLayout Root
// =========================
CATI2DLayoutLayout *pi2DLayout = NULL;
if (NULL_var != spPart)
{
  CAT2DLayoutServices::GetLayout2DL(spPart,&pi2DLayout);
  if (pi2DLayout)
  else
    Error_code = 71;
}
...

This section describes the standard way to retrieve the 2DLayout root from the Part document.

[Top]

 

Retrieves the 2D Layout Standard and Format of the Active Sheet

...
// 4/ Retrieves the 2D Layout standard and format of the active sheet
// ==================================================================
if (pi2DLayout)
{
  CATUnicodeString fmtName;
  CATBoolean sheetDisplay = FALSE;
  CATSheetOrientation sheetOrt;
  double fmtWidth=0.0;
  double fmtHeight=0.0;
  int indiceSheet = 0;
  hr = CAA2DLConvertToDrawServices::Get2DLStandardAndFormat(pi2DLayout, stdName,fmtWidth,fmtHeight,sheetOrt,sheetDisplay,indiceSheet);
  if (SUCCEEDED(hr))
  else
  {
    Error_code = 72;
  }
...

CAA2DLConvertToDrawServices class contains a set of services dedicated to this batch. Get2DLStandardAndFormat retrieves a set of characteristics of the 2D Layout for the drawing creation.

[Top]

Creating  and Initializing the new Drawing Document

...
 // 5/ Creates the new Drawing Document
// ===================================
CATDocument* pDocDraw = NULL;
hr = CATDocumentServices::New("CATDrawing", pDocDraw);
if (SUCCEEDED(hr))
else
{
  Error_code = 73;
}
// 6/ Drawing document initialisation: Standard, Sheet format and 2DLayout extraction
// ==================================================================================
hr = CAA2DLConvertToDrawServices::InitDrawingDocument(pDocDraw, stdName,fmtWidth,fmtHeight,sheetOrt,sheetDisplay,pi2DLayout);
if (SUCCEEDED(hr))
else
  Error_code = 74;

...

The InitDrawingDocument method defined in CAA2DLConvertToDrawServices class uses Extract2DLayoutToDrawing method defined in CAT2DLDrawingServices class to create drawing corresponding to the 2D Layout.

[Top]

Printing the Drawing Document by using the EPS print format

...
CATUnicodeString printfmt = "EPS";
CATUnicodeString drwfileName = dirToWork + "\\DrawingToPrint.CATDrawing";
CATUnicodeString printfileName = dirToWork + "\\PrintDrawing.ps";

// 7/ Print the Drawing document (Sheet number = indiceSheet)
// ==========================================================
// To print the active Sheet
int startLoop = indiceSheet;
int endLoop = indiceSheet+1;

// All the sheets are printed
CATUnicodeString AllSheets("AllSheets");
if (sSheetPrintMode == AllSheets)
{
  startLoop = 0;
  CATIUnknownList * pi2DLSheetList = NULL;
  if (SUCCEEDED(pi2DLayout->GetSheets(&pi2DLSheetList)) && pi2DLSheetList)
  {
    unsigned int piListSize = 0;
    pi2DLSheetList->Count(&piListSize);
    pi2DLSheetList->Release();pi2DLSheetList=NULL;
    endLoop = piListSize;
  }
}
for (int numSheet = startLoop; numSheet < endLoop; numSheet++)
{
  hr = CAA2DLConvertToDrawServices::PrintDrawingDocument(pDocDraw, printfileName, printfmt,numSheet);
  if (SUCCEEDED(hr))
  else
   Error_code = 75;
}

...

The PrintDrawingDocument method defined in CAA2DLConvertToDrawServices class use the method Print defined in CATPrintFileDevice class to create the "print file".

Launching the print file to the printer

...
// 8/ Launches the print to the printer
// ====================================
if (SUCCEEDED(hr))
{
#ifdef _WINDOWS_SOURCE

// Creates the .bat file to execute the print to the printer
const char* batFileName;
const char* tempDir = CATGetTempDirectory();
char* tempBatFileNameChar = _tempnam(tempDir, "printbatch");
CATUnicodeString tempBatFileName;
if (tempBatFileNameChar != NULL)
{
  tempBatFileName = tempBatFileNameChar;
  free(tempBatFileNameChar); 
}
tempBatFileName.Append(".bat");
batFileName = tempBatFileName.CastToCharPtr();

unsigned int batFileDesc;
HRESULT rc = CATFOpen(&tempBatFileName, "wt", &batFileDesc);
if ( SUCCEEDED(rc) )
{
  CATUnicodeString sSlash("\\");
  CATUnicodeString sBlanc(" ");
  CATUnicodeString slineString = "Copy /b ";
  slineString += printfileName;
  slineString += sBlanc;
  slineString += sSlash;
  slineString += sSlash;
  slineString += sPrinterSrvName;
  slineString += sSlash;
  slineString += sPrinterName;
  int nCharWritten = 0;
  rc = CATFPuts(slineString.ConvertToChar(), batFileDesc, &nCharWritten);
  rc = CATFClose(batFileDesc);
}
char* argv[2];
argv[0] = (char*) batFileName;
argv[1] = NULL;
int iPid,exitStatus;
CATLibStatus status = CATStartProcess(argv[0], argv, 0, &iPid, &exitStatus);
if (CATLibSuccess == status)
  Error_code = 0;
else
  Error_code = 76;
#else
// This step is not implemented on UNIX operating system
#endif

...

This section is only implemented for Windows operating system. The method CATStartProcess allows to start the printbatch.bat file

[Top]

Removing Document and Exiting

...
// 9/ Removes Drawing Document
// ===========================
  CATDocumentServices::Remove (*pDocDraw);
  }
  pi2DLayout->Release();pi2DLayout=NULL;
}

// 10/ Remove Part Document
// ========================
hr = CATUnLockDocument(*pDocPart);
CATDocumentServices::Remove (*pDocPart);
}
if (inputFiles) inputFiles->Release(),inputFiles = NULL;
if (inputParameters) inputParameters->Release(),inputParameters = NULL;

//11/ ENDS SESSION
//================
::Delete_Session("SampleSession");

This section represents the usual sequence to remove document of the session. The Part document must be unlocked before. In batch mode, it is very important to lock document when the batch works with several documents because the document could be released at any time.

[Top]


In Short

This use case shows the way to :

  1. Add a CAA command in the 2DL workbench.
  2. Launch a CAA Batch from an interactive command.
  3. Retrieving the 2DLayout root from the part document.
  4. Creating and Initializing a drawing document from a 2D Layout.
  5. Printing the drawing document in a "print file".
  6. Starting a ".bat" file to launch the print to the printer.
  7. Unlock the document and  close the session .

[Top]


References

[1] Creating a CAA V5 Batch
[Top]

History

Version: 1 [April 2008] Document created
[Top]

Copyright © 2008, Dassault Systèmes. All rights reserved.