Files
Hoops_Exchange/publish/publishsource/UpdateData/UpdateData.cpp
2025-12-15 22:10:55 +08:00

202 lines
7.8 KiB
C++

/***********************************************************************************************************************
*
* Copyright (c) 2010 - 2022 by Tech Soft 3D, Inc.
* The information contained herein is confidential and proprietary to Tech Soft 3D, Inc., and considered a trade secret
* as defined under civil and criminal statutes. Tech Soft 3D shall pursue its civil and criminal remedies in the event
* of unauthorized use or misappropriation of its trade secrets. Use of this information by anyone other than authorized
* employees of Tech Soft 3D, Inc. is granted only under a written non-disclosure agreement, expressly prescribing the
* scope and manner of such use.
*
***********************************************************************************************************************/
/**
\file UpdateData.cpp
This file demonstrates how to update a PDF file generated with Tetra 4D Enrich, using HOOPS Publish.
***********************************************************************************************************************/
#define INITIALIZE_A3D_API
#define HOOPS_PRODUCT_PUBLISH_ADVANCED
#include <A3DSDKIncludes.h>
#include "../common.hpp"
#include "../CommonInit.h"
#include <iostream>
// default sample arguments:
// in visual c++: the current directory is set through properties to $OutDir, which is $(Platform)\$(Configuration)
// in linux: the current directory is supposed to be the same as this cpp file
#ifdef _MSC_VER
# define IN_3DFILE SAMPLES_DATA_DIRECTORY"\\prc\\_micro engine.prc"
# define IN_PDFFILE SAMPLES_DATA_DIRECTORY"\\pdf_templates\\Tetra4D_Enrich_Template_PartList.pdf"
# define IN_XMLATTRIBS SAMPLES_DATA_DIRECTORY"\\prc\\_micro engine_Add3DAttributes.xml"
# define IN_XMLTEXTS SAMPLES_DATA_DIRECTORY"\\prc\\_micro engine_ImportTextField.xml"
# define IN_POSTERIMAGE SAMPLES_DATA_DIRECTORY"\\images\\empty.jpg"
# define OUT_FILE SAMPLES_PUBLISH_GALLERY_DIRECTORY"\\sample_UpdateData.pdf"
#else
# define MAX_PATH 4096 // let's be large
# define IN_3DFILE SAMPLES_DATA_DIRECTORY"/prc/_micro engine.prc"
# define IN_PDFFILE SAMPLES_DATA_DIRECTORY"/pdf_templates/Tetra4D_Enrich_Template_PartList.pdf"
# define IN_XMLATTRIBS SAMPLES_DATA_DIRECTORY"/prc/_micro engine_Add3DAttributes.xml"
# define IN_XMLTEXTS SAMPLES_DATA_DIRECTORY"/prc/_micro engine_ImportTextField.xml"
# define IN_POSTERIMAGE SAMPLES_DATA_DIRECTORY"/images/empty.jpg"
# define OUT_FILE SAMPLES_PUBLISH_GALLERY_DIRECTORY"/sample_UpdateData.pdf"
#endif
A3DStatus BuildPDFDocument(A3DUTF8Char *in_3dfile, A3DUTF8Char *out_pdffile)
{
A3DStatus iRet = A3D_SUCCESS;
// create document from template file.
// it is necessary to use the function A3DPDFDocumentCreateFromPDFFile to create a document that will be provided to A3DPDFDocumentUpdateData,
// so that all necessary information are kept(javascript and all internal data stored in the document).
A3DPDFDocument* pDoc = NULL;
CHECK_RET(A3DPDFDocumentCreateFromPDFFile(IN_PDFFILE, &pDoc));
if (pDoc != NULL)
{
// reading the input file; the file can be disposed of afterwards.
A3DAsmModelFile* pModelFile;
A3DRWParamsLoadData sReadParam;
A3D_INITIALIZE_DATA(A3DRWParamsLoadData, sReadParam);
sReadParam.m_sGeneral.m_bReadSolids = true;
sReadParam.m_sGeneral.m_bReadSurfaces = true;
sReadParam.m_sGeneral.m_bReadWireframes = true;
sReadParam.m_sGeneral.m_bReadPmis = true;
sReadParam.m_sGeneral.m_bReadAttributes = true;
sReadParam.m_sGeneral.m_bReadHiddenObjects = false;
sReadParam.m_sGeneral.m_bReadConstructionAndReferences = false;
sReadParam.m_sGeneral.m_bReadActiveFilter = true;
sReadParam.m_sGeneral.m_eReadingMode2D3D = kA3DRead_3D;
sReadParam.m_sGeneral.m_eReadGeomTessMode = kA3DReadGeomAndTess;
sReadParam.m_sGeneral.m_eDefaultUnit = kA3DUnitUnknown;
sReadParam.m_sPmi.m_bAlwaysSubstituteFont = false;
sReadParam.m_sPmi.m_pcSubstitutionFont = const_cast<A3DUTF8Char*>("Myriad CAD");
sReadParam.m_sTessellation.m_eTessellationLevelOfDetail = kA3DTessLODMedium;
sReadParam.m_sMultiEntries.m_bLoadDefault = true;
sReadParam.m_sAssembly.m_bUseRootDirectory = true;
iRet = A3DAsmModelFileLoadFromFile(in_3dfile, &sReadParam, &pModelFile);
if (iRet != A3D_SUCCESS && iRet != A3D_LOAD_MISSING_COMPONENTS)
{
std::cout << "Error number=" << iRet << std::endl;
return iRet;
}
if (pModelFile)
{
// creating a poster image to be used as a poster for 3D.
A3DPDFImage* pImage = NULL;
A3DPDFImageCreateFromFile(pDoc, IN_POSTERIMAGE, kA3DPDFImageFormatUnknown, &pImage);
// creating the 3D artwork
A3DPDF3DArtworkData2 s3DArtworkData;
A3D_INITIALIZE_DATA(A3DPDF3DArtworkData2, s3DArtworkData);
s3DArtworkData.m_sDisplaySectionData.m_bAddSectionCaps = true;
A3DPDF3DAnnotData sAnnotData;
A3D_INITIALIZE_DATA(A3DPDF3DAnnotData, sAnnotData);
sAnnotData.m_bOpenModelTree = false;
sAnnotData.m_bShowToolbar = false;
sAnnotData.m_eLighting = kA3DPDFLightCADOptimized;
sAnnotData.m_eRenderingStyle = kA3DPDFRenderingSolid;
sAnnotData.m_sBackgroundColor.m_dRed = 0.75;
sAnnotData.m_sBackgroundColor.m_dGreen = 0.75;
sAnnotData.m_sBackgroundColor.m_dBlue = 0.75;
sAnnotData.m_bTransparentBackground = false;
sAnnotData.m_eActivateWhen = kA3DPDFActivPageOpened;
sAnnotData.m_eDesactivateWhen = kA3DPDFActivPageClosed;
sAnnotData.m_iAppearanceBorderWidth = 0;
sAnnotData.m_pPosterImage = pImage;
// the modelfile pModelFile is stored in the PDF document as PRC, using these parameters
A3DRWParamsExportPrcData sParamExportPrcData;
A3D_INITIALIZE_DATA(A3DRWParamsExportPrcData, sParamExportPrcData);
A3DRWParamsPrcWriteHelper* pHelper = NULL;
iRet = A3DPDFDocumentUpdateData(pDoc,
NULL, -1, -1, // 3Dannot identifier: get the first one met in the document
pModelFile, &sParamExportPrcData, &pHelper, // modelfile to use to update 3D data
&s3DArtworkData, &sAnnotData, // options to create the replaced 3D annot
false, // bAddStandardViews
NULL, // pDefaultViewData
IN_XMLATTRIBS, // pcIn3dAttribsFile
IN_XMLTEXTS); // pcInTextFieldDataFile
// cleaning up -- WARNING: DO NOT CALL THIS BEFORE A3DPDF3DArtworkCreate!
CHECK_RET(A3DAsmModelFileDelete(pModelFile));
// saving the document
CHECK_RET(A3DPDFDocumentSaveEx(pDoc, kA3DPDFSaveFull | kA3DPDFSaveOptimized, out_pdffile));
CHECK_RET(A3DPDFDocumentClose(pDoc));
}
}
return iRet;
}
//######################################################################################################################
// Main function
#ifdef _MSC_VER
int wmain(A3DInt32 iArgc, A3DUniChar** ppcArgv)
#else
int main(int iArgc, A3DUTF8Char** ppcArgv)
#endif
{
#ifndef _MSC_VER
setenv("LC_NUMERIC", "en_US.UTF-8", 1); // Locally force locale
#endif
// init A3DLIB library - automatically handled init/terminate
A3DSDKHOOPSPublishLoader sHoopsPublishLoader(_T(HOOPS_BINARY_DIRECTORY));
CHECK_RET(sHoopsPublishLoader.m_eSDKStatus);
A3DDllSetCallbacksMemory(CheckMalloc, CheckFree);
// init HOOPS Publish related PDF library - automatically handled init/terminate
// do it only only once during the life of the application
CHECK_RET(sHoopsPublishLoader.InitPDFLib());
A3DUTF8Char in_3dfile[MAX_PATH * 4];
A3DUTF8Char out_pdffile[MAX_PATH * 4];
switch (iArgc)
{
case 1:
// no arguments provided
strcpy(in_3dfile, IN_3DFILE);
strcpy(out_pdffile, OUT_FILE);
break;
case 3:
// converting the input parameters to UTF-8
#ifdef _MSC_VER
A3DMiscUTF16ToUTF8(ppcArgv[1], in_3dfile);
A3DMiscUTF16ToUTF8(ppcArgv[2], out_pdffile);
#else
strcpy(in_3dfile, ppcArgv[1]);
strcpy(out_pdffile, ppcArgv[2]);
#endif
break;
default:
std::cout << "Usage:\n" << ppcArgv[0] << " <in_3dfile> <out_pdffile>\n";
return A3D_ERROR;
}
// launch PDF treatment
A3DStatus iRet = BuildPDFDocument(in_3dfile, out_pdffile);
if (iRet != A3D_SUCCESS)
return iRet;
// Check memory allocations
return (int)ListLeaks();
}