CreoPluging9.0/ShrinkwrapCommand.cpp
2026-01-16 10:08:15 +08:00

144 lines
4.2 KiB
C++

#include "pch.h"
#include "ShrinkwrapCommand.h"
#include "Creo90Manager.h"
#include "Shrinkwrap90Manager.h"
#include "ShrinkwrapDialog.h"
#include "CreoOtk.h"
#include <chrono>
#include <sstream>
namespace {
void ShowMessage(pfcSession_ptr session, const std::string& msg) {
try {
pfcMessageDialogOptions_ptr opts = pfcMessageDialogOptions::Create();
opts->SetMessageDialogType(pfcMESSAGE_INFO);
opts->SetDialogLabel("LightExport");
session->UIShowMessageDialog(msg.c_str(), opts);
} catch (...) {
}
}
std::string PromptForOutputPath(pfcSession_ptr session) {
try {
Creo90Manager creo;
pfcModel_ptr mdl = creo.GetCurrentModel();
std::string defaultName = "light";
if (mdl) {
xstring name = mdl->GetFileName();
const char* namePtr = static_cast<const char*>(name);
if (namePtr && namePtr[0]) {
defaultName = namePtr;
size_t dotPos = defaultName.rfind('.');
if (dotPos != std::string::npos) {
defaultName = defaultName.substr(0, dotPos);
}
defaultName += "_light";
}
}
pfcFileSaveOptions_ptr opts = pfcFileSaveOptions::Create("*.prt");
opts->SetPreselectedItem((defaultName + ".prt").c_str());
xstring result = session->UISaveFile(opts);
const char* resultPtr = static_cast<const char*>(result);
if (resultPtr && resultPtr[0]) {
return std::string(resultPtr);
}
} catch (...) {
}
return std::string();
}
} // namespace
void ShrinkwrapActionListener::OnCommand() {
try {
pfcSession_ptr session = pfcGetProESession();
if (!session) return;
Creo90Manager creo;
pfcSolid_ptr solid = creo.GetCurrentSolid();
if (!solid) {
ShowMessage(session, "No active part or assembly. Please open a model first.");
return;
}
ShrinkwrapShellRequest req;
req.quality = 5;
req.ignore_quilts = true;
req.ignore_skeleton = true;
req.assign_mass_properties = false;
// Show configuration dialog first
if (!ShrinkwrapDialog::Show(req)) {
return; // User cancelled
}
std::ostringstream debugOss;
const char* methodNames[] = { "Surfaces Only", "Approximate Solid", "Precise Solid" };
int mIdx = (req.method >= 0 && req.method <= 2) ? req.method : 0;
debugOss << "Parameters Applied:\n"
<< "Method: " << methodNames[mIdx] << "\n"
<< "Quality: " << req.quality << "\n"
<< "Fill Holes: " << (req.fill_holes ? "Yes" : "No") << "\n"
<< "Ignore Quilts: " << (req.ignore_quilts ? "Yes" : "No") << "\n"
<< "Ignore Skeleton: " << (req.ignore_skeleton ? "Yes" : "No") << "\n"
<< "Ignore Small: " << (req.ignore_small_surfaces ? "Yes" : "No");
ShowMessage(session, debugOss.str());
std::string outPath = PromptForOutputPath(session);
if (outPath.empty()) {
return;
}
req.output_file_path = outPath;
auto start = std::chrono::steady_clock::now();
Shrinkwrap90Manager mgr;
ShrinkwrapShellResult result = mgr.ExportShell(req);
auto end = std::chrono::steady_clock::now();
auto secs = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
result.execution_time = Shrinkwrap90Manager::FormatHhMmSs(secs);
std::ostringstream oss;
oss << "LightExport completed!\n\n";
oss << "Output: " << result.output_path << "\n";
oss << "File Size: " << result.file_size << "\n";
oss << "Time: " << result.execution_time;
ShowMessage(session, oss.str());
} catch (pfcXPFC& ex) {
try {
pfcSession_ptr session = pfcGetProESession();
xstring msg = ex.GetMessage();
const char* msgPtr = static_cast<const char*>(msg);
std::ostringstream oss;
oss << "Creo OTK Error: ";
if (msgPtr && msgPtr[0]) {
oss << msgPtr;
} else {
oss << "Unknown OTK error";
}
ShowMessage(session, oss.str());
} catch (...) {}
} catch (const std::exception& ex) {
try {
pfcSession_ptr session = pfcGetProESession();
std::ostringstream oss;
oss << "Error: " << ex.what();
ShowMessage(session, oss.str());
} catch (...) {}
} catch (...) {
try {
pfcSession_ptr session = pfcGetProESession();
ShowMessage(session, "Unknown error occurred");
} catch (...) {}
}
}