#include #include #include #include #include #include "SketchUpAPI/sketchup.h" int main() { SUInitialize(); //CreateModel SUModelRef model = SU_INVALID; SUResult result = SUModelCreate( &model ); assert(result == SU_ERROR_NONE); //get entities SUEntitiesRef entities = SU_INVALID; result = SUModelGetEntities(model, &entities); assert(result == SU_ERROR_NONE); //Create list of vertices SUGeometryInputRef geom_input = SU_INVALID; result = SUGeometryInputCreate(&geom_input); assert(result == SU_ERROR_NONE); SUPoint3D* points = new SUPoint3D[36]; // TRIANGLE 1 { SUPoint3D pt1 = {10.0, 10.0, 10.0} ; SUPoint3D pt2 = {0, 10.0, 10.0} ; SUPoint3D pt3 = {10.0, 0, 10.0} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt1); SUGeometryInputAddVertex(geom_input, &pt2); SUGeometryInputAddVertex(geom_input, &pt3); } //TRIANGLE 2 { SUPoint3D pt4 = {10.0, 0, 10.0} ; SUPoint3D pt5 = {0, 10, 10} ; SUPoint3D pt6 = {0, 0, 10} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt4); SUGeometryInputAddVertex(geom_input, &pt5); SUGeometryInputAddVertex(geom_input, &pt6); } //TRIANGLE 3 { SUPoint3D pt7 = {10, 0, 0} ; SUPoint3D pt8 = {0, 0, 0} ; SUPoint3D pt9 = {10, 10, 0} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt7); SUGeometryInputAddVertex(geom_input, &pt8); SUGeometryInputAddVertex(geom_input, &pt9); } //TRIANGLE 4 { SUPoint3D pt10 = {10, 10, 0} ; SUPoint3D pt11 = {0, 0, 0} ; SUPoint3D pt12 = {0, 10, 0} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt10); SUGeometryInputAddVertex(geom_input, &pt11); SUGeometryInputAddVertex(geom_input, &pt12); } //TRIANGLE 5 { SUPoint3D pt13 = {0, 0, 10} ; SUPoint3D pt14 = {0, 0, 0} ; SUPoint3D pt15 = {10, 0, 10} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt13); SUGeometryInputAddVertex(geom_input, &pt14); SUGeometryInputAddVertex(geom_input, &pt15); } //Triangle 6 { SUPoint3D pt16 = {10, 0, 10} ; SUPoint3D pt17 = {0, 0, 0} ; SUPoint3D pt18 = {10, 0, 0} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt16); SUGeometryInputAddVertex(geom_input, &pt17); SUGeometryInputAddVertex(geom_input, &pt18); } //Triangle 7 { SUPoint3D pt19 = {0, 10, 10} ; SUPoint3D pt20 = {0, 10, 0} ; SUPoint3D pt21 = {0, 0, 10} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt19); SUGeometryInputAddVertex(geom_input, &pt20); SUGeometryInputAddVertex(geom_input, &pt21); } //Triangle 8 { SUPoint3D pt22 = {0, 0, 10} ; SUPoint3D pt23 = {0, 10, 0} ; SUPoint3D pt24 = {0, 0, 0} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt22); SUGeometryInputAddVertex(geom_input, &pt23); SUGeometryInputAddVertex(geom_input, &pt24); } //Triangle 9 { SUPoint3D pt25 = {10, 10, 10} ; SUPoint3D pt26 = {10, 10, 0} ; SUPoint3D pt27 = {0, 10, 10} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt25); SUGeometryInputAddVertex(geom_input, &pt26); SUGeometryInputAddVertex(geom_input, &pt27); } //Triangle 10 { SUPoint3D pt28 = {0, 10, 10} ; SUPoint3D pt29 = {10, 10, 0} ; SUPoint3D pt30 = {0, 10, 0} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt28); SUGeometryInputAddVertex(geom_input, &pt29); SUGeometryInputAddVertex(geom_input, &pt30); } //Triangle 11 { SUPoint3D pt31 = {10, 0, 10} ; SUPoint3D pt32 = {10, 0, 0} ; SUPoint3D pt33 = {10, 10, 10} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt31); SUGeometryInputAddVertex(geom_input, &pt32); SUGeometryInputAddVertex(geom_input, &pt33); } //Triangle 12 { SUPoint3D pt34 = {10, 10, 10} ; SUPoint3D pt35 = {10, 0, 0} ; SUPoint3D pt36 = {10, 10, 0} ; //Add to SUGeometryInputRef SUGeometryInputAddVertex(geom_input, &pt34); SUGeometryInputAddVertex(geom_input, &pt35); SUGeometryInputAddVertex(geom_input, &pt36); } //Create basic standard material SUMaterialRef mMaterial; { // init color SUSetInvalid( mMaterial ); SUMaterialCreate(&mMaterial); const SUColor color = { LOBYTE( 130 ), LOBYTE( 130 ), LOBYTE( 130 ), 255 }; SUMaterialSetColor(mMaterial, &color); //Add material SUModelAddMaterials(model, 1, &mMaterial); } // Create faces: faces were tessellated into a triangular mesh { const size_t num_triangles = 12; size_t face_vertex_count = 0; size_t global_vertex_count = 0; for (size_t tri_index = 0; tri_index < num_triangles; ++tri_index) { // Set up an outer loop input for the face SULoopInputRef loop = SU_INVALID; result = SULoopInputCreate( &loop ) ; assert(result == SU_ERROR_NONE); for (size_t i = 0; i < 3; ++i) { SULoopInputAddVertexIndex(loop, global_vertex_count + i); } // Add the face size_t face_index = 0; result = SUGeometryInputAddFace( geom_input, &loop, &face_index ) ; assert(result == SU_ERROR_NONE); // Set material to the face SUMaterialRef material = mMaterial; if ( SUIsValid( material ) ) { SUMaterialInput mat_input = SU_INVALID; mat_input.material = material; SUGeometryInputFaceSetFrontMaterial(geom_input, face_index, &mat_input); SUGeometryInputFaceSetBackMaterial(geom_input, face_index, &mat_input); } global_vertex_count += 3; } } //Create entities from geom_input result = SUEntitiesFill(entities, geom_input, true); assert(result == SU_ERROR_NONE); SUGeometryInputRelease(&geom_input); //smooth model - remove unecessary edge (cojacent edges) { size_t edgeCount = 0; SUEntitiesGetNumEdges( entities, false, &edgeCount ); SUEdgeRef* edges = new SUEdgeRef[ edgeCount ]; SUEntitiesGetEdges( entities, false, edgeCount, edges, &edgeCount ); for ( unsigned long index = 0; index < edgeCount; ++index ) { SUEdgeRef edge = edges[index]; SUIsValid( edge ); //Get faces count on a edge size_t faceCount = 0; SUEdgeGetNumFaces( edge, &faceCount ); if ( faceCount == 2 ) { SUFaceRef* faces = new SUFaceRef[ faceCount ]; SUEdgeGetFaces( edge, faceCount, faces, &faceCount ); SUFaceRef face1 = faces[0]; SUFaceRef face2 = faces[1]; //Get face1 normal struct SUVector3D normal1; SUFaceGetNormal(face1, &normal1); //Get face2 normal struct SUVector3D normal2; SUFaceGetNormal(face2, &normal2); //Compute angle between both normals double angle = 0.0; { SUVector3D crossProductP = { normal1.y*normal2.z - normal1.z*normal2.y, normal1.z*normal2.x - normal1.x*normal2.z, normal1.x*normal2.y - normal1.y*normal2.x }; double normCrossProduct = std::sqrt( crossProductP.x*crossProductP.x + crossProductP.y*crossProductP.y + crossProductP.z*crossProductP.z ); double dotProduct = normal1.x*normal2.x + normal1.y*normal2.y + normal1.z*normal2.z; angle = std::atan2( normCrossProduct, dotProduct ); } static const double level = 0.349066; //20 degrees const bool smoothed = angle < level; SUEdgeSetSmooth( edge, smoothed ); SUEdgeSetSoft (edge, true); delete[] faces; } } delete[] edges; } //Create group name SUGroupRef mGroup = SU_INVALID; SUGroupCreate( &mGroup ); SUGroupSetName( mGroup, "TEST GROUP NAME" ); SUEntitiesAddGroup(entities, mGroup); // SUComponentInstanceSaveAs SUModelSaveToFileWithVersion(model, "C:\\Users\\sho\\Downloads\\SUCubeCreation.skp", SUModelVersion_SU2016); assert(result == SU_ERROR_NONE); SUModelRelease(&model); SUTerminate(); return 0; }