Now I have tried to duplicate the issue using the code shown below, and it crashes during the call to LODocumentExportToPDF…
All hard-coded numbers are coming from a bigger piece code and should be correct.
I also upload two files
1)“Habran-Bercheux_M0-01.skp” which is the basic model I use, without any section plane defined in it.
2)“Habran-Bercheux_M0-01_sub.skp” which is the model that I save after having defined a scene and a section plane in it.
Could you make sure the scene and the section plane are defined correctly in this model?
Then what could be the origin of the crash?
Thanks in advance,
#include <iostream>
#include <vector>
#include<SketchupAPI/initialize.h>
#include<SketchupAPI/sketchup.h>
#include <LayOutAPI/initialize.h>
#include <LayOutAPI/layout.h>
int main()
{
SUInitialize();
LOInitialize();
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// Importing model from .skp file
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "CREATE MODEL FROM FILE..." << std::endl;
std::string file_path =
"C://Users//regae//Documents//Paille_Tech//sketchup_api//skp_files//test//Habran-Bercheux_M0-01.skp";
SUModelLoadStatus loadStatus = SUModelLoadStatus_Success;
SUModelRef model = SU_INVALID;
SUResult res = SUModelCreateFromFileWithStatus(&model, file_path.c_str(), &loadStatus);
if (res != SU_ERROR_NONE) std::cout << "TEST 1 FAILED" << std::endl;
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// Create a scene and add it to the model
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "CREATE SCENE..." << std::endl;
SUSceneRef scene = SU_INVALID;
SUSceneCreate(&scene);
std::string scene_name = "TOP_VIEW_SCENE";
SUSceneSetName(scene, scene_name.c_str());
int sceneIndex;
res = SUModelAddScene(model, -1, scene, &sceneIndex);
if (res != SU_ERROR_NONE) std::cout << "TEST 2 FAILED" << std::endl;
sceneIndex++; //needed for LOSketchupModelSetCurrentScene
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// Create a camera and make the scene use it
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "CREATE CAMERA..." << std::endl;
SUCameraRef camera = SU_INVALID;
SUCameraCreate(&camera);
SUVector3D up; up.x = 0; up.y = -1; up.z = 0;
SUPoint3D pos; pos.x = 263.307; pos.y = 9.921; pos.z = 114.724;
SUPoint3D tg; tg.x = pos.x; tg.y = pos.y; tg.z = 0;
SUCameraSetOrientation(camera, &pos, &tg, &up);
SUCameraSetPerspective(camera, false);
SUCameraSet2D(camera, true);
SUCameraSetOrthographicFrustumHeight(camera, 21.1);
res = SUSceneSetCamera(scene, camera);
if (res != SU_ERROR_NONE) std::cout << "TEST 3 FAILED" << std::endl;
SUSceneSetUseCamera(scene, true);
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// Create a section plane
// Add it to the model's entities
// Make it the model's active section plane
// Make the scene use section planes
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "CREATE SECTION PLANE..." << std::endl;
SUPlane3D plane;
SUPlane3DSetWithCoefficients(&plane, 0, 0, -1, 10);
SUSectionPlaneRef sectionPlane = SU_INVALID;
SUSectionPlaneCreate(§ionPlane);
SUSectionPlaneSetPlane(sectionPlane, &plane);
std::string name = "top_scene_section";
SUSectionPlaneSetName(sectionPlane, name.c_str());
SUEntitiesRef entities = SU_INVALID;
SUModelGetEntities(model, &entities);
std::vector<SUSectionPlaneRef> planes;
planes.push_back(sectionPlane);
res = SUEntitiesAddSectionPlanes(entities, 1, &planes[0]);
if (res != SU_ERROR_NONE) std::cout << "TEST 4 FAILED" << std::endl;
res = SUEntitiesSetActiveSectionPlane(entities, sectionPlane);
if (res != SU_ERROR_NONE) std::cout << "TEST 5 FAILED" << std::endl;
SUSceneSetUseSectionPlanes(scene,true);
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// Save the model to another file
// to be used in LODocument
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "SAVING MODEL TO ANOTHER FILE..." << std::endl;
std::string file_path2 =
"C://Users//regae//Documents//Paille_Tech//sketchup_api//skp_files//test//Habran-Bercheux_M0-01_tmp.skp";
SUModelVersion version = SUModelVersion_SU2021;
res = SUModelSaveToFileWithVersion(model, file_path2.c_str(), version);
if (res != SU_ERROR_NONE) std::cout << "TEST 6 FAILED" << std::endl;
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// Create an empty LODocument
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "CREATE EMPTY DOCUMENT..." << std::endl;
LODocumentRef document = SU_INVALID;
LODocumentCreateEmpty(&document);
LOLayerRef defaultLayer = SU_INVALID;
LODocumentAddLayer(document, false /* shared */, &defaultLayer);
LOLayerSetName(defaultLayer, "Default");
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// Create a LOSketchUpModel using the saved model
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "CREATE LOSKETCHUPMODEL FROM FILE..." << std::endl;
LOSketchUpModelRef lomodel = SU_INVALID;
LOAxisAlignedRect2D bounds;
double INCH = 2.54;
double margin = 1. / INCH;
double page_x = 21. / INCH;
double page_y = 29.7 / INCH;
bounds.upper_left.x = margin;
bounds.upper_left.y = margin;
bounds.lower_right.x = page_x - margin;
bounds.lower_right.y = page_y - margin;
res = LOSketchUpModelCreate(&lomodel, file_path2.c_str(),&bounds);
if (res != SU_ERROR_NONE) std::cout << "TEST 7 FAILED" << std::endl;
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// add the scene view to the LODocument's first page
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "ADDING SCENE TO DOCUMENT..." << std::endl;
LOPageRef page = SU_INVALID;
LODocumentGetPageAtIndex(document, 0, &page);
res = LOSketchUpModelSetCurrentScene(lomodel, sceneIndex);
if (res != SU_ERROR_NONE) std::cout << "TEST 8 FAILED" << std::endl;
LOEntityRef entity = LOSketchUpModelToEntity(lomodel);
LODocumentAddEntity(document, entity, defaultLayer, page);
bounds.upper_left.x = 1.814;
bounds.upper_left.y = 7.898;
bounds.lower_right.x = 6.454;
bounds.lower_right.y = 11.595;
double scale = 1. / 29.56;
res = LOEntitySetUntransformedBounds(entity, &bounds);
if (res != SU_ERROR_NONE) std::cout << "TEST 9 FAILED" << std::endl;
res = LOSketchUpModelSetScale(lomodel, scale);
if (res != SU_ERROR_NONE) std::cout << "TEST 10 FAILED" << std::endl;
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// Export LODocument to pdf and layout
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "EXPORT RESULT IN PDF AND LAYOUT FORMATS..." << std::endl;
std::string file_path_pdf =
"C://Users//regae//Documents//Paille_Tech//sketchup_api//skp_files//test//Habran-Bercheux_M0-01.pdf";
std::string file_path_lo =
"C://Users//regae//Documents//Paille_Tech//sketchup_api//skp_files//test//Habran-Bercheux_M0-01.layout";
LODictionaryRef dictionary = SU_INVALID;
std::cout << "test A" << std::endl;
LODocumentExportToPDF(document, file_path_pdf.c_str(), dictionary);
std::cout << "test B" << std::endl;
LODocumentSaveToFile(document, file_path_lo.c_str(), LODocumentVersion_Current);
std::cout << "test C" << std::endl;
//++++++++++++++++++++++++++++++++++++++++++++++++++++
// Terminate program
//++++++++++++++++++++++++++++++++++++++++++++++++++++
std::cout << "TERMINATE PROGRAM..." << std::endl;
std::remove(file_path2.c_str());
LODocumentRelease(&document);
LOSketchUpModelRelease(&lomodel);
SUModelRelease(&model);
SUTerminate();
LOTerminate();
}
```[Habran-Bercheux_M0-01.skp|attachment](upload://soFRZaxiCGvQFq9fT5xJFrfwwl0.skp) (87.0 KB)
[Habran-Bercheux_M0-01_tmp.skp|attachment](upload://z0nXbNupoHyEBpzLMSiY8qQwdcp.skp) (87.3 KB)