Skip to content

Commit

Permalink
noetic support, based on InigoMoreno's work. see ros-visualization#39
Browse files Browse the repository at this point in the history
  • Loading branch information
k-okada committed Jul 20, 2022
1 parent 1ec4886 commit 077b4e2
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 50 deletions.
87 changes: 56 additions & 31 deletions smach_viewer/scripts/smach_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,63 @@
import pickle
import pprint
import copy
import StringIO
try:
from StringIO import StringIO # for Python 2
except ImportError:
from io import StringIO # for Python 3
import colorsys
import time
import base64

import wxversion
if wxversion.checkInstalled("2.8"):
wxversion.select("2.8")
else:
print("wxversion 2.8 is not installed, installed versions are {}".format(wxversion.getInstalled()))
try:
import wxversion
if wxversion.checkInstalled("2.8"):
wxversion.select("2.8")
else:
print("wxversion 2.8 is not installed, installed versions are {}".format(wxversion.getInstalled()))

## this import system (or ros-released) xdot
# import xdot
## need to import currnt package, but not to load this file
# http://stackoverflow.com/questions/6031584/importing-from-builtin-library-when-module-with-same-name-exists
def import_non_local(name, custom_name=None):
import imp, sys

custom_name = custom_name or name

path = filter(lambda x: x != os.path.dirname(os.path.abspath(__file__)), sys.path)
f, pathname, desc = imp.find_module(name, path)

module = imp.load_module(custom_name, f, pathname, desc)
if f:
f.close()

return module

smach_viewer = import_non_local('smach_viewer')
from smach_viewer import xdot
from smach_viewer.xdot import wxxdot
from smach_viewer.xdot.xdot import TextShape
except:
# Guard against self import
this_dir = os.path.dirname(__file__)
# Use os.getcwd() to aovid weird symbolic link problems
cur_dir = os.getcwd()
os.chdir(this_dir)
this_dir_cwd = os.getcwd()
os.chdir(cur_dir)
# Remove this dir from path
sys.path = [a for a in sys.path if a not in [this_dir, this_dir_cwd]]
#
from smach_viewer.xdot import wxxdot
from xdot.ui.elements import *


import wx
import wx.richtext

import textwrap

## this import system (or ros-released) xdot
# import xdot
## need to import currnt package, but not to load this file
# http://stackoverflow.com/questions/6031584/importing-from-builtin-library-when-module-with-same-name-exists
def import_non_local(name, custom_name=None):
import imp, sys

custom_name = custom_name or name

path = filter(lambda x: x != os.path.dirname(os.path.abspath(__file__)), sys.path)
f, pathname, desc = imp.find_module(name, path)

module = imp.load_module(custom_name, f, pathname, desc)
if f:
f.close()

return module

smach_viewer = import_non_local('smach_viewer')
from smach_viewer import xdot
##
import smach
import smach_ros
Expand Down Expand Up @@ -167,8 +189,11 @@ def _load_local_data(self, msg):
"""Unpack the user data"""
try:
local_data = pickle.loads(msg.local_data)
except KeyError:
local_data = pickle.loads(base64.b64decode(msg.local_data.encode('utf-8')))
except:
if isinstance(msg.local_data, str):
local_data = pickle.loads(base64.b64decode(msg.local_data))
else:
local_data = pickle.loads(base64.b64decode(bytes(str(msg.local_data).encode('utf-8'))))
return local_data

def update_status(self, msg):
Expand Down Expand Up @@ -460,7 +485,7 @@ def set_styles(self, selected_paths, depth, max_depth, items, subgraph_shapes, c
else:
if child_path in items:
for shape in items[child_path].shapes:
if not isinstance(shape,xdot.xdot.TextShape):
if not isinstance(shape,TextShape):
shape.pen.color = child_color
shape.pen.fillcolor = child_fillcolor
shape.pen.linewidth = child_linewidth
Expand Down Expand Up @@ -565,7 +590,7 @@ def __init__(self):
self.Bind(wx.EVT_TOOL, self.SaveDotGraph, id=wx.ID_SAVE)

# Create dot graph widget
self.widget = xdot.wxxdot.WxDotWindow(graph_view, -1)
self.widget = wxxdot.WxDotWindow(graph_view, -1)

gv_vbox.Add(toolbar, 0, wx.EXPAND)
gv_vbox.Add(self.widget, 1, wx.EXPAND)
Expand Down Expand Up @@ -1071,4 +1096,4 @@ def main():
if __name__ == '__main__':
rospy.init_node('smach_viewer',anonymous=False, disable_signals=True,log_level=rospy.INFO)
sys.argv = rospy.myargv()
main()
main()
2 changes: 1 addition & 1 deletion smach_viewer/setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

from distutils.core import setup
from setuptools import setup
from catkin_pkg.python_setup import generate_distutils_setup

d = generate_distutils_setup(
Expand Down
1 change: 1 addition & 0 deletions smach_viewer/src/smach_viewer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

75 changes: 57 additions & 18 deletions smach_viewer/src/smach_viewer/xdot/wxxdot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,67 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from .xdot import *

try:
from xdot.ui.elements import *
from xdot.ui.animation import *
from xdot.dot.lexer import *
from xdot.dot.parser import *
import subprocess
except:
from xdot import *

# Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.
if sys.version_info[0] >= 3:
unicode = str

__all__ = ['WxDotWindow', 'WxDotFrame']

# We need to get the wx version with built-in cairo support
import wxversion
if wxversion.checkInstalled("2.8"):
wxversion.select("2.8")
else:
print("wxversion 2.8 is not installed, installed versions are {}".format(wxversion.getInstalled()))
try:
import wxversion
if wxversion.checkInstalled("2.8"):
wxversion.select("2.8")
else:
print("wxversion 2.8 is not installed, installed versions are {}".format(wxversion.getInstalled()))
except:
pass # Python3

import wx
import wx.lib.wxcairo as wxcairo

# This is a crazy hack to get this to work on 64-bit systems
if 'wxMac' in wx.PlatformInfo:
pass # Implement if necessary
elif 'wxMSW' in wx.PlatformInfo:
pass # Implement if necessary
elif 'wxGTK' in wx.PlatformInfo:
import ctypes
gdkLib = wx.lib.wxcairo._findGDKLib()
gdkLib.gdk_cairo_create.restype = ctypes.c_void_p
try:
if 'wxMac' in wx.PlatformInfo:
pass # Implement if necessary
elif 'wxMSW' in wx.PlatformInfo:
pass # Implement if necessary
elif 'wxGTK' in wx.PlatformInfo:
import ctypes
gdkLib = wx.lib.wxcairo._findGDKLib()
gdkLib.gdk_cairo_create.restype = ctypes.c_void_p
except:
pass # Python3

class MyXDotParser(XDotParser):

def __init__(self, xdotcode):
XDotParser.__init__(self,xdotcode)
self.subgraph_shapes = {}

def parse_subgraph(self):
shapes_before = set(self.shapes)

id = XDotParser.parse_subgraph(self)

new_shapes = set(self.shapes) - shapes_before
self.subgraph_shapes[id.decode()] = [s for s in new_shapes if not any([s in ss for ss in self.subgraph_shapes.values()])]

return id

def parse(self):
graph = XDotParser.parse(self)
graph.subgraph_shapes = self.subgraph_shapes
return graph

class WxDragAction(object):
def __init__(self, dot_widget):
Expand Down Expand Up @@ -436,8 +474,9 @@ def set_filter(self, filter):
self.filter = filter

def set_dotcode(self, dotcode, filename='<stdin>'):
if isinstance(dotcode, unicode):
dotcode = dotcode.encode('utf8')
# Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.
if sys.version_info[0] < 3 and isinstance(dotcode, unicode):
dotcode = dotcode.encode('utf8')
p = subprocess.Popen(
[self.filter, '-Txdot'],
stdin=subprocess.PIPE,
Expand Down Expand Up @@ -484,7 +523,7 @@ def set_dotcode(self, dotcode, filename='<stdin>'):
def set_xdotcode(self, xdotcode):
"""Set xdot code."""
#print xdotcode
parser = XDotParser(xdotcode)
parser = MyXDotParser(bytes(str(xdotcode).encode("utf-8")))
self.graph = parser.parse()
self.highlight = None
#self.zoom_image(self.zoom_ratio, center=True)
Expand Down

0 comments on commit 077b4e2

Please sign in to comment.