Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the support for running the creo2urdf headless #86

Merged
merged 5 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/creo2urdf/include/creo2urdf/Creo2Urdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class Creo2Urdf : public pfcUICommandActionListener {
*/
void OnCommand() override;

Creo2Urdf() = default;
~Creo2Urdf() = default;
Creo2Urdf(const std::string& yaml_path, const std::string& csv_path, const std::string& output_path, pfcModel_ptr asm_model_ptr) : m_yaml_path(yaml_path),
m_csv_path(csv_path),
m_output_path(output_path),
m_asm_model_ptr(asm_model_ptr) { }

private:
/**
* @brief Export the iDynTree model to URDF format if it is valid.
Expand Down Expand Up @@ -137,7 +144,10 @@ class Creo2Urdf : public pfcUICommandActionListener {
std::array<double, 3> originXYZ {0.0, 0.0, 0.0}; /**< Offset of the root link in XYZ (meters) wrt the world frame. */
std::array<double, 3> originRPY {0.0, 0.0, 0.0}; /**< Orientation of the root link in Roll-Pitch-Yaw wrt the world frame. */
bool warningsAreFatal{ true }; /**< Flag indicating whether warnings are treated as fatal errors. */
std::string m_yaml_path{ "" }; /**< Path to the YAML configuration file. */
std::string m_csv_path{ "" }; /**< Path to the CSV file containing joint information. */
std::string m_output_path{ "" }; /**< Output path for the exported URDF file. */
pfcModel_ptr m_asm_model_ptr{ nullptr }; /**< Handle to the Creo model. */
};

class Creo2UrdfAccess : public pfcUICommandAccessListener {
Expand Down
53 changes: 37 additions & 16 deletions src/creo2urdf/src/Creo2Urdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@
void Creo2Urdf::OnCommand() {

pfcSession_ptr session_ptr = pfcGetProESession();
pfcModel_ptr model_ptr = session_ptr->GetCurrentModel();
if (!session_ptr) {
printToMessageWindow("Failed to get the session", c2uLogLevel::WARN);
return;
}
if (!m_asm_model_ptr) {
m_asm_model_ptr = session_ptr->GetCurrentModel();
if (!m_asm_model_ptr) {
printToMessageWindow("Failed to get the current model", c2uLogLevel::WARN);
return;
}
}


// TODO Principal units probably to be changed from MM to M before getting the model properties
Expand All @@ -31,23 +41,28 @@ void Creo2Urdf::OnCommand() {
auto yaml_file_open_option = pfcFileOpenOptions::Create("*.yml,*.yaml");
yaml_file_open_option->SetDialogLabel("Select the yaml");

auto yaml_path = session_ptr->UIOpenFile(yaml_file_open_option);

if (!loadYamlConfig(string(yaml_path)))
// YAML file path
if (m_yaml_path.empty()) {
m_yaml_path = string(session_ptr->UIOpenFile(yaml_file_open_option));
}
if (!loadYamlConfig(m_yaml_path))
{
printToMessageWindow("Failed to run Creo2Urdf!", c2uLogLevel::WARN);
return;
}
auto csv_file_open_option = pfcFileOpenOptions::Create("*.csv");
csv_file_open_option->SetDialogLabel("Select the csv");

auto csv_path = session_ptr->UIOpenFile(csv_file_open_option);

rapidcsv::Document joints_csv_table(string(csv_path), rapidcsv::LabelParams(0, 0));
auto output_folder_open_option = pfcDirectorySelectionOptions::Create();
output_folder_open_option->SetDialogLabel("Select the output dir");

m_output_path = string(session_ptr->UISelectDirectory(output_folder_open_option));
// CSV file path
if (m_csv_path.empty()) {
auto csv_file_open_option = pfcFileOpenOptions::Create("*.csv");
csv_file_open_option->SetDialogLabel("Select the csv");
m_csv_path = string(session_ptr->UIOpenFile(csv_file_open_option));
}
rapidcsv::Document joints_csv_table(m_csv_path, rapidcsv::LabelParams(0, 0));
// Output folder path
if (m_output_path.empty()) {
auto output_folder_open_option = pfcDirectorySelectionOptions::Create();
output_folder_open_option->SetDialogLabel("Select the output dir");
m_output_path = string(session_ptr->UISelectDirectory(output_folder_open_option));
}
printToMessageWindow("Output path is: " + m_output_path);


Expand All @@ -56,7 +71,7 @@ void Creo2Urdf::OnCommand() {

bool ret;

auto asm_component_list = model_ptr->ListItems(pfcModelItemType::pfcITEM_FEATURE);
auto asm_component_list = m_asm_model_ptr->ListItems(pfcModelItemType::pfcITEM_FEATURE);
if (asm_component_list->getarraysize() == 0) {
printToMessageWindow("There are no FEATURES in the asm", c2uLogLevel::WARN);
return;
Expand Down Expand Up @@ -117,7 +132,7 @@ void Creo2Urdf::OnCommand() {

elem_tree.populateJointInfoFromElementTree(feat, joint_info_map);

pfcComponentPath_ptr comp_path = pfcCreateComponentPath(pfcAssembly::cast(model_ptr), seq);
pfcComponentPath_ptr comp_path = pfcCreateComponentPath(pfcAssembly::cast(m_asm_model_ptr), seq);

auto component_handle = session_ptr->RetrieveModel(pfcComponentFeat::cast(feat)->GetModelDescr());

Expand Down Expand Up @@ -358,6 +373,12 @@ void Creo2Urdf::OnCommand() {

exportModelToUrdf(idyn_model, export_options);

// Let's clear the map in case of multiple click TODO UNIFY
m_yaml_path.clear();
m_csv_path.clear();
m_output_path.clear();
m_asm_model_ptr = nullptr;

return;
}

Expand Down
58 changes: 58 additions & 0 deletions src/creo2urdf/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@

#include <creo2urdf/Creo2Urdf.h>
#include <creo2urdf/Validator.h>
#include <ProTKRunTime.h>
#include <ProCore.h>
#include <pfcExceptions.h>


/*! @brief Do batch mode stuff
*/
ProError evaluateBatchMode(const std::string& asm_path, const std::string& yaml_path, const std::string& csv_path, const std::string& output_path) {
if (asm_path.empty() || yaml_path.empty() || csv_path.empty() || output_path.empty()) {
return PRO_TK_BAD_INPUTS; // to be safe
}
ProError err = PRO_TK_NO_ERROR;

pfcBaseSession* session = pfcGetProESession();
if (!session) {
ProTKPrintf("Creo2Urdf: impossible to retrieve the session");
return PRO_TK_GENERAL_ERROR;
}

pfcModel_ptr asm_model_ptr{ nullptr };

try {
asm_model_ptr = session->RetrieveModel(pfcModelDescriptor::CreateFromFileName(asm_path.c_str()));
}
xcatchbegin
xcatchcip(defaultEx)
{
ProTKPrintf("Exception caught: %s", pfcXPFC::cast(defaultEx)->GetMessage());
return PRO_TK_E_NOT_FOUND;
}
xcatchend

Creo2Urdf creo2urdfApp(yaml_path, csv_path, output_path, asm_model_ptr);
creo2urdfApp.OnCommand();
return err;
}

/**
* @brief Initializes the buttons by associating them with the corresponding functions
Expand All @@ -23,6 +59,28 @@ extern "C" int user_initialize(
{
auto session = pfcGetProESession();

if (argc > 4) {
std::string asm_path = argv[1];
std::string yaml_path = argv[2];
std::string csv_path = argv[3];
std::string output_path = argv[4];

// We need to remove the '+' character from the paths
asm_path.erase(std::find(asm_path.begin(), asm_path.end(), '+'));
yaml_path.erase(std::find(yaml_path.begin(), yaml_path.end(), '+'));
csv_path.erase(std::find(csv_path.begin(), csv_path.end(), '+'));
output_path.erase(std::find(output_path.begin(), output_path.end(), '+'));

ProTKPrintf("Running in batch mode");
auto debug_msg = "Assembly path: " + asm_path + " yaml path " + yaml_path + " csv_path " + csv_path + " output_path " + output_path;
ProTKPrintf("%s\n", debug_msg.c_str());
ProError err = evaluateBatchMode(asm_path, yaml_path, csv_path, output_path);
ProEngineerEnd();
return (int)err; // or whatever you want
}

ProTKPrintf("Creo2Urdf version %s build %s\n", version, build);

auto cmd = session->UICreateCommand("Creo2Urdf", new Creo2Urdf());
cmd->AddActionListener(new Creo2UrdfAccess()); // To be checked it is odd
cmd->Designate("ui.txt", "Run Creo2Urdf", "Run Creo2Urdf", "Run Creo2Urdf");
Expand Down