I also forgot to mention that I got everything to work just be commenting out the export manager.
My problem now is rendering.
My computer crashes after it gets up to around 10GB of Ram while running the script.
I can generate it in parts but no all together without a GPU
Any ideas on how to run this thing?
Maybe somethings configured wrong
#define _USE_MATH_DEFINES
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <stack>
#include <random>
#include <vector>
using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;
using namespace std;
ofstream myfile;
struct hole {
double x, y, z, dia;
bool isReal;
};
vector<hole> holes;
hole temp;
double degrees = 0;
double _poleLength = 2.54 * 8 * 12;
double _gapTop = 2.54 * 6;
double _gapBottom = 2.54 * 6;
double _poleOD = 2.54 * 1;
double _poleID = 2.54 * .75;
double _slices = 12;
double _sliceArcLength = M_PI * _poleOD / _slices;
double _radius = _poleOD / 2;
double _bitL = 2.54 * 5 / 16;
double _bitM = 2.54 * 1 / 4;
double _bitS = 2.54 * 3 / 16;
int _numHolesL = 2000;
int _numHolesM = 1500;
int _numHolesS = 1000;
Ptr<Application> app = Application::get();
Ptr<UserInterface> ui = app->userInterface();
bool createToolBody()
{
Ptr<Documents> docs = app->documents();
// Create a document.
Ptr<Document> doc = docs->add(DocumentTypes::FusionDesignDocumentType);
Ptr<Design> design = app->activeProduct();
// Get the root component of the active design
Ptr<Component> rootComp = design->rootComponent();
// Create sketch
Ptr<Sketches> sketches = rootComp->sketches();
Ptr<Sketch> sketch = sketches->add(rootComp->yZConstructionPlane());
// Create sketch circle
Ptr<SketchCurves> curves = sketch->sketchCurves();
Ptr<SketchCircles> circles = curves->sketchCircles();
Ptr<Point3D> centerPoint = Point3D::create(0, 0, 0);
circles->addByCenterRadius(centerPoint, _poleID);
circles->addByCenterRadius(centerPoint, _poleOD);
// Get the profile defined by the circle
Ptr<Profiles> profs = sketch->profiles();
Ptr<Profile> prof = profs->item(1);
//// Create an extrusion input
Ptr<Features> features = rootComp->features();
Ptr<ExtrudeFeatures> extrudes = features->extrudeFeatures();
Ptr<ExtrudeFeatureInput> extInput = extrudes->createInput(prof, FeatureOperations::NewBodyFeatureOperation);
// Define that the extent is a distance extent of 8 ft
Ptr<ValueInput> distance = ValueInput::createByReal(_poleLength);
// Set the distance extent to be one-way symmetric
extInput->setDistanceExtent(false, distance);
// Set the extrude to be a solid one
extInput->isSolid(true);
// Create an cylinder
Ptr<ExtrudeFeature> extrude = extrudes->add(extInput);
/*
// Save and reopen the document to work a bug around.
Ptr<ExportManager> exportMgr = design->exportManager();
Ptr<FusionArchiveExportOptions> exportOption = exportMgr->createFusionArchiveExportOptions("C:\\temp.f3d");
exportMgr->execute(exportOption);
doc->close(false);
Ptr<ImportManager> importMgr = app->importManager();
Ptr<FusionArchiveImportOptions> importOption = importMgr->createFusionArchiveImportOptions("C:\\temp.f3d");
importMgr->importToNewDocument(importOption);
*/
return true;
}
void makeCylinder() {
Ptr<Design> design = app->activeProduct();
Ptr<Component> rootComp = design->rootComponent();
Ptr<ConstructionPlanes> planes = rootComp->constructionPlanes();
Ptr<ConstructionPlaneInput> planeInput = planes->createInput();
// Do the magic cylinder stuff
Ptr<BRepBodies> bodies = rootComp->bRepBodies();
Ptr<BRepBody> body = bodies->item(0);
Ptr<BRepFaces> faces = body->faces();
Ptr<BRepFace> cylinderFace;
for (auto face : faces)
{
Ptr<Cylinder> cylinder = face->geometry();
if (cylinder && std::fabs(cylinder->radius() - _poleOD) < 1.0e-6)
{
cylinderFace = face;
break;
}
}
Ptr<Sketches> sketches = rootComp->sketches();
Ptr<Sketch> sketch = sketches->add(rootComp->yZConstructionPlane());
Ptr<SketchPoints> points = sketch->sketchPoints();
Ptr<Point3D> pickPt = Point3D::create(2, 0, 1);
Ptr<SketchPoint> pickSketchPt = points->add(sketch->modelToSketchSpace(pickPt));
planeInput->setByTangentAtPoint(cylinderFace, pickSketchPt);
Ptr<ConstructionPlane> plane = planes->add(planeInput);
if (!plane)
{
ui->messageBox("Failed to create tangent work plane by cylinder face.");
//return false;
}
}
/*
void drillHole(double x, double y, double z) {
Ptr<Design> design = app->activeProduct();
Ptr<Component> rootComp = design->rootComponent();
Ptr<ConstructionPlanes> planes = rootComp->constructionPlanes();
Ptr<SketchPoints> points = sketch->sketchPoints();
//Create new plane hole
planes->createInput();
Ptr<Point3D>pickPt = Point3D::create(x, y, z);
//points->release();
Ptr<SketchPoint>pickSketchPt = points->add(sketch->modelToSketchSpace(pickPt));
planeInput->setByTangentAtPoint(cylinderFace, pickSketchPt);
plane = planes->add(planeInput);
holeInput->setPositionByPoint(plane, Point3D::create(x, y, z));
holes->add(holeInput);
}
*/
void makeCandyCane(){
//Candy Cane Coordinated
for (temp.x = _gapBottom; temp.x <= _poleLength - _gapTop; temp.x += _bitL) {
temp.y = cos(degrees);
temp.z = sin(degrees);
holes.push_back(temp);
//holes.pop_back();
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
temp.y = cos(degrees + .5);
temp.z = sin(degrees + .5);
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
temp.y = cos(degrees + 1);
temp.z = sin(degrees + 1);
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
temp.x += _bitL;
degrees += .25;
}myfile << "\nOK - Candy Cane\n";
}
void makePhantomHoles(){
//Create Phantom Candy Cane Holes
myfile << "Create Phantom Holes\n";
temp.isReal = false;
double stop = (double)holes.size() - 3;
for (int count = 0; count < stop; count += 3)
{
//(0)=>(1) left hole -> middle hole same row
temp.x = (holes[count + 1].x - holes[count].x) / 2 + holes[count].x;
temp.y = (holes[count + 1].y - holes[count].y) / 2 + holes[count].y;
temp.z = (holes[count + 1].z - holes[count].z) / 2 + holes[count].z;
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
//(0)=>(3) left hole -> uprow left
temp.x = (holes[count + 3].x - holes[count].x) / 2 + holes[count].x;
temp.y = (holes[count + 3].y - holes[count].y) / 2 + holes[count].y;
temp.z = (holes[count + 3].z - holes[count].z) / 2 + holes[count].z;
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
//(1)=>(3) middle hole -> uprow left
temp.x = (holes[count + 3].x - holes[count + 1].x) / 2 + holes[count + 1].x;
temp.y = (holes[count + 3].y - holes[count + 1].y) / 2 + holes[count + 1].y;
temp.z = (holes[count + 3].z - holes[count + 1].z) / 2 + holes[count + 1].z;
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
//(1)=>(4) middle hole -> uprow middle
temp.x = (holes[count + 4].x - holes[count + 1].x) / 2 + holes[count + 1].x;
temp.y = (holes[count + 4].y - holes[count + 1].y) / 2 + holes[count + 1].y;
temp.z = (holes[count + 4].z - holes[count + 1].z) / 2 + holes[count + 1].z;
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
//(1)=>(2) middle hole -> right same row
temp.x = (holes[count + 2].x - holes[count + 1].x) / 2 + holes[count + 1].x;
temp.y = (holes[count + 2].y - holes[count + 1].y) / 2 + holes[count + 1].y;
temp.z = (holes[count + 2].z - holes[count + 1].z) / 2 + holes[count + 1].z;
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
//(2)=>(4) right hole -> uprow middle hole
temp.x = (holes[count + 4].x - holes[count + 2].x) / 2 + holes[count + 2].x;
temp.y = (holes[count + 4].y - holes[count + 2].y) / 2 + holes[count + 2].y;
temp.z = (holes[count + 4].z - holes[count + 2].z) / 2 + holes[count + 2].z;
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
//(2)=>(5) right hole -> uprow right hole
temp.x = (holes[count + 5].x - holes[count + 2].x) / 2 + holes[count + 2].x;
temp.y = (holes[count + 5].y - holes[count + 2].y) / 2 + holes[count + 2].y;
temp.z = (holes[count + 5].z - holes[count + 2].z) / 2 + holes[count + 2].z;
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
myfile << "\n Count " << count << "\t Lights Size " << holes.size() << "\n";
if (count < stop && count > stop - 6) {
//Fill in last row of fake holes
myfile << "\nFill last row of fake holes";
//(3)=>(4) up left -> up middle
temp.x = (holes[count + 4].x - holes[count + 3].x) / 2 + holes[count + 3].x;
temp.y = (holes[count + 4].y - holes[count + 3].y) / 2 + holes[count + 3].y;
temp.z = (holes[count + 4].z - holes[count + 3].z) / 2 + holes[count + 3].z;
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
//(4)=>(5) up middle -> up right
temp.x = (holes[count + 5].x - holes[count + 4].x) / 2 + holes[count + 4].x;
temp.y = (holes[count + 5].y - holes[count + 4].y) / 2 + holes[count + 4].y;
temp.z = (holes[count + 5].z - holes[count + 4].z) / 2 + holes[count + 4].z;
holes.push_back(temp);
myfile << "\n[" << holes.size() - 1 << "]\t" << holes[holes.size() - 1].x << "\t" << holes[holes.size() - 1].y << "\t" << holes[holes.size() - 1].z << "\t" << holes[holes.size() - 1].dia << "\t" << holes[holes.size() - 1].isReal;
myfile << "\n Count " << count << "\t Lights Size " << holes.size() << "\n";
}
}
}
void makeRandomHoles(double num, double size) {
temp.dia = size;
temp.isReal = true;
for (int count = 0; count < num; count++) {
//Pick Random X, Y & Z 3Dpoints
//myfile << "\nCheck out Random Temp Light Hole";
degrees = rand() % 360;
temp.x = (double)(rand() % (int)(_poleLength - _gapBottom - _gapTop)) + _gapBottom;
temp.y = cos(degrees) * _radius;
temp.z = sin(degrees) * _radius;
myfile << "\n[temp]\t" << temp.x << "\t" << temp.y << "\t" << temp.z << "\t" << temp.dia << "\t" << temp.isReal;
//Check random point distance to all other current points
for (int x = 0; x < holes.size(); x++) {
if (sqrt(pow(temp.x - holes[x].x, 2) + pow(temp.y - holes[x].y, 2) + pow(temp.z - holes[x].z, 2)) > _bitL*2.5) {
holes.push_back(temp);
myfile << "\n[" << holes.size() << "][" << x << "] Degrees " << degrees << "\t" << temp.x << "\t" << temp.y << "\t" << temp.z << "\t" << temp.dia << "\t" << temp.isReal;
break;
}
}
}
}
void drillHole(double x, double y, double z, double drillBit) {
Ptr<Design> design = app->activeProduct();
Ptr<Component> rootComp = design->rootComponent();
Ptr<ConstructionPlanes> planes = rootComp->constructionPlanes();
Ptr<ConstructionPlaneInput> planeInput = planes->createInput();
Ptr<BRepBodies> bodies = rootComp->bRepBodies();
Ptr<BRepBody> body = bodies->item(0);
Ptr<BRepFaces> faces = body->faces();
Ptr<BRepFace> cylinderFace;
for (auto face : faces)
{
Ptr<Cylinder> cylinder = face->geometry();
if (cylinder && std::fabs(cylinder->radius() - _poleOD) < 1.0e-6)
{
cylinderFace = face;
break;
}
}
Ptr<Sketches> sketches = rootComp->sketches();
Ptr<Sketch> sketch = sketches->add(rootComp->yZConstructionPlane());
Ptr<SketchPoints> points = sketch->sketchPoints();
Ptr<Point3D> pickPt = Point3D::create(x, y, z);
Ptr<SketchPoint> pickSketchPt = points->add(sketch->modelToSketchSpace(pickPt));
planeInput->setByTangentAtPoint(cylinderFace, pickSketchPt);
Ptr<ConstructionPlane> plane = planes->add(planeInput);
// Create a hole input
double stroke = 1;
Ptr<Features> features = rootComp->features();
Ptr<HoleFeatures> holes = features->holeFeatures();
Ptr<HoleFeatureInput> holeInput = holes->createSimpleInput(ValueInput::createByReal(drillBit)); // If you use a variable you don't get a hole
holeInput->setPositionByPoint(plane, Point3D::create(x, y, z));
holeInput->setDistanceExtent(adsk::core::ValueInput::createByReal(_poleOD / 2));
Ptr<HoleFeature> hole = holes->add(holeInput);
}
extern "C" XI_EXPORT bool run(const char* context)
{
myfile.open("debug033101.txt");
srand((unsigned)time(NULL));
myfile << std::to_string(rand()) + " Random Initialized\n";
myfile << M_PI << " Pi works\n";
//hole temp;
temp.x = _gapBottom + _bitL / 2;
temp.y = cos(degrees);
temp.z = sin(degrees);
temp.dia = _bitL;
temp.isReal = true;
myfile << temp.x << "\t" << temp.y << "\t" << temp.z << "\t" << temp.dia << "\t" << temp.isReal;
myfile << "OK - Variable Initialization\n";
if (!createToolBody()) return false;
myfile << "OK - Created Pole\n";
makeCylinder();
myfile << "OK - Created Cylinder\n";
makeCandyCane();
makePhantomHoles();
makeRandomHoles(_numHolesL, _bitL);
makeRandomHoles(_numHolesM, _bitM);
makeRandomHoles(_numHolesS, _bitS);
//Pop drill stack
for (int count = 0; count < holes.size(); count++) {
if (holes[count].isReal == true) {
drillHole(holes[count].x, holes[count].y, holes[count].z, holes[count].dia);
myfile << "[" << count << "] Popped\t" << holes[count].x << "\t" << holes[count].y << "\t" << holes[count].z << "\n";
}
}/*
for (int count = 405; count <455; count++) {
if (holes[count].isReal == false) {
drillHole(holes[count].x, holes[count].y, holes[count].z, holes[count].dia);
myfile << "[" << count << "] Popped\t" << holes[count].x << "\t" << holes[count].y << "\t" << holes[count].z << "\n";
}
}*/
/*
for (int count = 1350; count <1450; count++) {
if (holes[count].isReal == true) {
drillHole(holes[count].x, holes[count].y, holes[count].z, holes[count].dia);
myfile << "[" << count << "] Popped\t" << holes[count].x << "\t" << holes[count].y << "\t" << holes[count].z << "\n";
}
}*/
return true;
}
#ifdef XI_WIN
#include <windows.h>
BOOL APIENTRY DllMain(HMODULE hmodule, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif // XI_WIN