Skip to content

Commit b7447df

Browse files
ahcordeadlarkin
andauthored
USD -> SDF: Added cmd line tool (#862)
Signed-off-by: ahcorde <ahcorde@gmail.com> Co-authored-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com>
1 parent 346f3ae commit b7447df

4 files changed

Lines changed: 170 additions & 0 deletions

File tree

usd/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ target_link_libraries(${usd_target}
3131

3232
set(gtest_sources
3333
sdf_parser/sdf2usd_TEST.cc
34+
usd_parser/usd2sdf_TEST.cc
3435
sdf_parser/Geometry_Sdf2Usd_TEST.cc
3536
sdf_parser/Joint_Sdf2Usd_TEST.cc
3637
sdf_parser/Light_Sdf2Usd_TEST.cc

usd/src/cmd/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@ if(TARGET ${usd_target})
99
${usd_target}
1010
)
1111

12+
add_executable(usd2sdf
13+
usd2sdf.cc
14+
)
15+
16+
target_link_libraries(usd2sdf
17+
PUBLIC
18+
ignition-utils${IGN_UTILS_VER}::ignition-utils${IGN_UTILS_VER}
19+
${usd_target}
20+
)
21+
1222
install(
1323
TARGETS
1424
sdf2usd
25+
usd2sdf
1526
DESTINATION
1627
${BIN_INSTALL_DIR}
1728
)

usd/src/cmd/usd2sdf.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (C) 2022 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
#include <iostream>
19+
#include <string>
20+
21+
#include <ignition/utils/cli/CLI.hpp>
22+
23+
#include "sdf/config.hh"
24+
25+
//////////////////////////////////////////////////
26+
/// \brief Enumeration of available commands
27+
enum class Command
28+
{
29+
kNone,
30+
};
31+
32+
//////////////////////////////////////////////////
33+
/// \brief Structure to hold all filenames
34+
struct Options
35+
{
36+
/// \brief Command to execute
37+
Command command{Command::kNone};
38+
39+
/// \brief input filename
40+
std::string inputFilename{"input.usd"};
41+
42+
/// \brief output filename
43+
std::string outputFilename{"output.sdf"};
44+
};
45+
46+
void runCommand(const Options &/*_opt*/)
47+
{
48+
// TODO(ahcorde): Call here the USD to SDF converter code
49+
std::cerr << "USD to SDF conversion is not implemented yet.\n";
50+
}
51+
52+
void addFlags(CLI::App &_app)
53+
{
54+
auto opt = std::make_shared<Options>();
55+
56+
_app.add_option("input",
57+
opt->inputFilename,
58+
"Input filename. Defaults to input.usd unless otherwise specified.");
59+
60+
_app.add_option("output",
61+
opt->outputFilename,
62+
"Output filename. Defaults to output.sdf unless otherwise specified.");
63+
64+
_app.callback([&_app, opt](){
65+
runCommand(*opt);
66+
});
67+
}
68+
69+
//////////////////////////////////////////////////
70+
int main(int argc, char** argv)
71+
{
72+
CLI::App app{"USD to SDF converter"};
73+
74+
app.set_help_all_flag("--help-all", "Show all help");
75+
76+
app.add_flag_callback("--version", [](){
77+
std::cout << SDF_VERSION_FULL << std::endl;
78+
throw CLI::Success();
79+
});
80+
81+
addFlags(app);
82+
CLI11_PARSE(app, argc, argv);
83+
84+
return 0;
85+
}

usd/src/usd_parser/usd2sdf_TEST.cc

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2022 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
#include <string>
18+
19+
#include <gtest/gtest.h>
20+
21+
#include <ignition/common/Filesystem.hh>
22+
#include <ignition/utilities/ExtraTestMacros.hh>
23+
24+
#include "test_config.h"
25+
#include "test_utils.hh"
26+
27+
#ifdef _WIN32
28+
#define popen _popen
29+
#define pclose _pclose
30+
#endif
31+
32+
static std::string usd2sdfCommand()
33+
{
34+
return ignition::common::joinPaths(std::string(PROJECT_BINARY_DIR), "bin",
35+
"usd2sdf");
36+
}
37+
38+
/////////////////////////////////////////////////
39+
std::string custom_exec_str(std::string _cmd)
40+
{
41+
_cmd += " 2>&1";
42+
FILE *pipe = popen(_cmd.c_str(), "r");
43+
44+
if (!pipe)
45+
return "ERROR";
46+
47+
char buffer[128];
48+
std::string result = "";
49+
50+
while (!feof(pipe))
51+
{
52+
if (fgets(buffer, 128, pipe) != NULL)
53+
result += buffer;
54+
}
55+
56+
pclose(pipe);
57+
return result;
58+
}
59+
60+
/////////////////////////////////////////////////
61+
TEST(version_cmd, IGN_UTILS_TEST_DISABLED_ON_WIN32(SDF))
62+
{
63+
// Check a good SDF file
64+
{
65+
std::string output =
66+
custom_exec_str(usd2sdfCommand() + " --version");
67+
68+
EXPECT_EQ(output, std::string(SDF_VERSION_FULL) + "\n");
69+
70+
// TODO(ahcorde): Check the contents of outputUsdFilePath when the parser
71+
// is implemented
72+
}
73+
}

0 commit comments

Comments
 (0)