Skip to main content

a simple XML macro script

Project description

xmacro

This package is refactor version of xacro4sdf. (WIP)

xmacro is a simple tool to define and parse XML macro. it's inspired by ros/xacro which is an XML macro language desiged for urdf, xmacro looks like simplified version of ros/xacro , but it's more simple, and also more easy to use.

  • xmacro is independent of ROS, you could install it by pip .
  • XML namespaces isn't used in xmacro , and there are some reserved words for xmacro : xmacro_include, xmacro_define_value, xmacro_define_block, xmacro_block.
  • it provides python api so that we could parse xml file in ROS2 launch file.
  • it provides xmacro4sdf : general xmacro with some specific function for sdf.

Usage

Installation

# install from source code
git clone https://github.com/gezp/xmacro.git
cd xmacro && sudo python3 setup.py install

examples

# some examples in folder test/
xmacro test_xmacro.xml.xmacro > test_xmacro.xml

XMLMacro Features

  • Value macro
  • Block macro
  • Math expressions
  • Include
  • Python API

Value macro

Value macro are named values that can be inserted anywhere into the XML document except <xmacro_define_block> block.

xmacro definition

<!--definition of properties -->
<xmacro_define_value name="radius" value="4.3" />
<!--use of properties-->
<circle diameter="${2 * radius}" />

generated xml

<circle diameter="8.6" />

Block macro

Define block macros with the macro tag <xmacro_define_block>, then specify the macro name and a list of parameters. The list of parameters should be whitespace separated.

The usage of block macros is to define <xmacro_block> which will be replaced with corresponding <xmacro_define_block> block.

xmacro definition

<!--definition of macro-->
	<xmacro_define_value name="mass" value="0.2" />
	<xmacro_define_block macro_name="box_inertia" params="m x y z">
        <mass>${m}</mass>
        <inertia>
            <ixx>${m*(y*y+z*z)/12}</ixx>
            <ixy>0</ixy>
            <ixz>0</ixz>
            <iyy>${m*(x*x+z*z)/12}</iyy>
            <iyz>0</iyz>
            <izz>${m*(x*x+z*z)/12}</izz>
        </inertia>
    </xmacro_define_block>
<!--use of macro-->
    <inertial>
         <pose>0 0 0.02 0 0 0</pose>
         <xmacro_block name="box_inertia" m="${mass}" x="0.3" y="0.1" z="0.2"/>
    </inertial>

generated xml

			<inertial>
				<pose>0 0 0.02 0 0 0</pose>
				<mass>0.2</mass>
				<inertia>
					<ixx>0.0008333333333333335</ixx>
					<ixy>0</ixy>
					<ixz>0</ixz>
					<iyy>0.002166666666666667</iyy>
					<iyz>0</iyz>
					<izz>0.002166666666666667</izz>
				</inertia>
			</inertial>
  • only support simple parameters (string and number), and block parameters isn't supported.
  • it's supported to use other xmacro_block in xmacro_define_block which is recursive definition.

it's not recommended to define macro recursively (only support <=5).

Math expressions

  • within dollared-braces ${xxxx}, you can also write simple math expressions.
  • refer to examples of **Value macro ** and **Block macro **
  • it's implemented by calling eval() in python, so it's unsafe for some cases.

Including other xmacro files

You can include other xmacro files by using the <xmacro_include> tag.

  • it will include the xmcaro definition with tag <xmacro_define_value> and macros with tag <xmacro_define_block>.
<xmacro_include uri="file://simple_car/model.sdf.xmacro"/>
  • The uri for file means to open the file directly.
    • it try to open the file with relative path simple_car/model.sdf.xmacro .
    • you can also try to open file with absolute path /simple_car/model.sdf.xmacro with uri file:///simple_car/model.sdf.xmacro.
  • <xmacro_include> supports to include recursively.

Python API

you can use xmacro in python easily

from xmacro.xmacro import XMLMacro

xmacro=XMLMacro()
#case1 parse from file
xmacro.set_xml_file(inputfile)
xmacro.generate()
xmacro.to_file(outputfile)

#case2 parse from xml string
xmacro.set_xml_string(xml_str)
xmacro.generate()
xmacro.to_file(outputfile)

#case3 generate to string
xmacro.set_xml_file(inputfile)
xmacro.generate()
xmacro.to_string()

#case4 custom property
xmacro.set_xml_file(inputfile)
# use custom property dictionary to overwrite global property  
# defined by <xacro_define_property> (only in 'inputfile')
kv={"rplidar_a2_h":0.8}
xmacro.generate(kv)
xmacro.to_file(outputfile)

XMLMacro4sdf Features

pre-defined common.sdf.xmacro

<!--macro defination:inertia-->
<xmacro_define_block name="inertia_cylinder" params="m r l">
<xmacro_define_block name="inertia_box" params="m x y z">
<xmacro_define_block name="inertia_sphere" params="m r">
<!--macro defination:geometry-->
<xmacro_define_block name="geometry_cylinder" params="r l">
<xmacro_define_block name="geometry_box" params="x y z">
<xmacro_define_block name="geometry_sphere" params="r">
<xmacro_define_block name="geometry_mesh" params="uri">
<!--macro defination:visual_collision_with_mesh-->
<xmacro_define_block name="visual_collision_with_mesh" params="prefix uri">

(TODO)

Maintainer and License

maintainer : Zhenpeng Ge, zhenpeng.ge@qq.com

xmacro is provided under MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xmacro-1.0.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distributions

xmacro-1.0.0-py3.8.egg (14.6 kB view details)

Uploaded Source

xmacro-1.0.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file xmacro-1.0.0.tar.gz.

File metadata

  • Download URL: xmacro-1.0.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

File hashes

Hashes for xmacro-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2482cd6544811388229c5631fad7758b6f6055ced6b2e5cd4ab299bedfb45b51
MD5 07cf84d8a7e4ec66e361f8b3edafa179
BLAKE2b-256 19ace8111026940393108ceb2473ee51deee16d75dd8a53c3ab23e627ffa842f

See more details on using hashes here.

File details

Details for the file xmacro-1.0.0-py3.8.egg.

File metadata

  • Download URL: xmacro-1.0.0-py3.8.egg
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

File hashes

Hashes for xmacro-1.0.0-py3.8.egg
Algorithm Hash digest
SHA256 626ad0c291f9f7e5e72d96d930e4e51254a66a2fc68237db3ae8d9232ff920ea
MD5 d1df2e1919597e5c380aaa9ea0f87bd5
BLAKE2b-256 91f60260d9c21d84deb4b2e86a302c8675604e573248f171a65c9586b96f8558

See more details on using hashes here.

File details

Details for the file xmacro-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: xmacro-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

File hashes

Hashes for xmacro-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5fcf51b72f78f4c2a20e7480a75c94d7ba5a9c4c4e6dda6bd809b6595fbf5a1d
MD5 b6da50359e7b50ba9641e80530bc9a9d
BLAKE2b-256 257d67ac39a1e604767933bc7017901bdba34c83e3d15b0487f8f828d62c0f46

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page