Skip to content

Commit

Permalink
Merge pull request #59 from ZEISS/20240919-update-scripted-diagram-ba…
Browse files Browse the repository at this point in the history
…sics

Updated after changes in main SW
  • Loading branch information
mprinkezs authored Oct 1, 2024
2 parents 6c8d4e6 + 2968290 commit 0da967b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

## Short description

This example includes a script for generation of scripted actual circles, which can provide data — their radius — to any of two scripted diagram services. The diagram service `RadiusPlot` plots the radius of each circle element, the diagram service `RadiusHistogram` creates a radius histogram.

> [!WARNING]
> Only one service function may be used at a time.
> All diagram services currently not in use must be stopped, otherwise no diagram is created.
This example includes a script for generating scripted actual circles, which can provide data — their radius — to any of two scripted diagram services. The diagram service `RadiusPlot` plots the radius of each circle element, the diagram service `RadiusHistogram` creates a radius histogram.

## Prerequisite

Expand All @@ -25,15 +21,16 @@ The radius of any scripted circle is passed as a parameter to the scripted diagr
context.data[stage] = {
"ude_diagram_custom": 1,
"ude_diagram_type": "SVGDiagram",
# Selected by circle creation dialog: "gom.api.diagram.radius_plot" or "gom.api.statistics.radius_histogram"
"ude_diagram_service" : params['service'],
# Selected by circle creation dialog:
# polisher = 'gom.api.diagram.radius_plot' or polisher = 'gom.api.statistics.radius_histogram'
"ude_diagram_service" : polisher,
"ude_diagram_radius": params['radius']
}
```

## Managing the scripted diagram services

Use Apps->Manage Services... to start either `RadiusPlot` or `RadiusHistogram` and stop any diagram service currently not in use. Only one diagram service may be active!
Use Apps->Manage Services... to start `RadiusPlot` and/or `RadiusHistogram`.

## Diagram settings

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes OSMMapDiagram
# Release Notes ScriptedDiagramBasics

## Installation Requirements

* Software Version
* ZEISS INSPECT 2025

## Released at 2024-09-30 (v1.0.0)

* Initial release
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,34 @@
# ---
"""Scripted diagram service creating a radius histogram"""

import io
import gom
import gom.api.settings
from gom import apifunction
import matplotlib.pyplot as plt

# Set path for debugging
SVG_PATH = None
#SVG_PATH = 'C:/temp/OSMMapDiagram.svg'
from gom import apifunction

# Set SVG resolution in dpi
SVG_DPI = 'figure'
import matplotlib.pyplot as plt
import gom.api.extensions.diagrams.matplotlib_tools as mpltools

@apifunction
def radius_histogram(*args, **kwargs)->str:
"""Plot element name/radius
Args:
args (any): (unused)
kwargs (dict): {'<uuid>': {'ude_diagram_custom': 1, 'ude_diagram_radius': <radius>, ...}, ...}
Returns:
string: SVG image
def radius_histogram(view, element_data)->str:
"""
Plot radius histogram
"""
gom.log.info('Radius Histogram Service')
gom.log.info(f'{kwargs=}')
gom.log.info(f'{view=}, {element_data=}')

bins = gom.api.settings.get('bins')
bins = [int(elem) for elem in bins.split(',') if elem.strip().isnumeric()]

mpltools.setup_plot (plt, view)

radius = []

for uuid, params in kwargs.items():
radius.append(params['ude_diagram_radius'])
for e in element_data:
data = e['data']

gom.log.debug(f'{bins=}')
gom.log.debug(f'{radius=}')
radius.append(data['ude_diagram_radius'])

plt.figure(figsize = (10, 5))
color = gom.api.settings.get('barcolor')

# Creating the histogram plot
Expand All @@ -74,21 +65,6 @@ def radius_histogram(*args, **kwargs)->str:
xlabels.append(f'[{bins[i]},{bins[i+1]}]')
plt.xticks(xticks, xlabels)

if SVG_PATH:
plt.savefig(SVG_PATH, format='svg', dpi=SVG_DPI)

# Create an empty file-like object
svg_output = io.StringIO()

# Save the plot to the file-like object
plt.savefig(svg_output, format='svg', dpi=SVG_DPI)

# Get the SVG string from the file-like object
svg_string = svg_output.getvalue()

# Close the file-like object
svg_output.close()

return svg_string
return mpltools.create_svg (plt, view)

gom.run_api()
gom.run_api()
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
#
# radius_histogram.py
# radius_plot.py
#
# Service function which receives the radii of all elements plots
# Service function which receives the radii of all elements and plots
# radius and element name.
# See https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
#
Expand All @@ -13,76 +13,32 @@
# ---
"""Scripted diagram service plotting radius / element name"""

import io
import gom
from gom import apifunction
import matplotlib.pyplot as plt


# Set path for debugging
SVG_PATH = None
#SVG_PATH = 'C:/temp/ScriptedDiagram.svg'

# Set SVG resolution in dpi
SVG_DPI = 'figure'

def filter_all(k, v):
"""Filter all elements by key, value
Args:
k (string): key
v (any): value
from gom import apifunction

Returns:
list: element references
"""
r = []
for g in [gom.app.project.nominal_elements, gom.app.project.inspection, gom.app.project.actual_elements]:
r += g.filter(k, v)
return r
import matplotlib.pyplot as plt
import gom.api.extensions.diagrams.matplotlib_tools as mpltools

@apifunction
def radius_plot(*args, **kwargs)->str:
"""Plot a radius histogram
Args:
args (any): (unused)
kwargs (dict): {'<uuid>': {'ude_diagram_custom': 1, 'ude_diagram_radius': <radius>, ...}, ...}
Returns:
string: SVG image
def radius_plot(view, element_data)->str:
"""
Plot circle radius marks
"""
gom.log.info('Radius Plot Service')
gom.log.info(f'{kwargs=}')
gom.log.info(f'{view=}, {element_data=}')

radius = []
elementnames = []
for uuid, params in kwargs.items():
element = filter_all('uuid_draft', uuid)[0]
elementnames.append(element.name)
radius.append(params['ude_diagram_radius'])
mpltools.setup_plot (plt, view)

# create x/y plot
plt.figure(figsize = (10, 5))
plt.plot(elementnames, radius, 'bx')
plt.xticks(rotation = 90)
plt.subplots_adjust(bottom=0.2)
for e in element_data:
element = e['element']
data = e['data']

if SVG_PATH:
plt.savefig(SVG_PATH, format='svg', dpi=SVG_DPI)
plt.plot ([element.name], [data['ude_diagram_radius']], 'bx')

# Create an empty file-like object
svg_output = io.StringIO()

# Save the plot to the file-like object
plt.savefig(svg_output, format='svg', dpi=SVG_DPI)

# Get the SVG string from the file-like object
svg_string = svg_output.getvalue()

# Close the file-like object
svg_output.close()
plt.xticks(rotation = 90)

return svg_string
return mpltools.create_svg (plt, view)

gom.run_api()
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def dialog_event_handler(widget):

DIALOG.handler = dialog_event_handler
# -------------------------------------------------------------------------
RESULT = gom.script.sys.show_user_defined_dialog(dialog=DIALOG)
gom.script.sys.show_user_defined_dialog(dialog=DIALOG)
return params

# -------------------------------------------------------------------------
Expand All @@ -88,13 +88,23 @@ def calculation(context, params):
'direction': (params['dir_x'], params['dir_y'], params['dir_z']),
'radius': params['radius']
}

polisher = None
if params['service'].lower () == 'radius':
polisher = 'gom.api.diagram.radius_plot'
elif params['service'].lower () == 'histogram':
polisher = 'gom.api.statistics.radius_histogram'
else:
raise RuntimeError ('Unknown polisher service')

context.data[stage] = {
"ude_diagram_custom": 1,
"ude_diagram_type": "SVGDiagram",
# "gom.api.diagram.radius_plot" or "gom.api.statistics.radius_histogram"
"ude_diagram_service" : params['service'],
"ude_diagram_service" : polisher,
"ude_diagram_radius": params['radius'],
"ude_diagram_center": gom.Vec3d(params['center_x'], params['center_y'], params['center_z'])
"ude_diagram_center": gom.Vec3d(
params['center_x'], params['center_y'], params['center_z']
)
}
print(f'{context.data[stage]}')
except Exception as error:
Expand Down

0 comments on commit 0da967b

Please sign in to comment.