microstation

MicroStation expects scripts with the ‘.txt’ extension. Scripts can be run by typing @ followed by the DOS short name path to you’re script in the keyin editor. For more information see the MicroStation help. See “Ask Inga” for more detailed usage help.

Wrapper for MicroStation command script generation.

classes

Layer

class pygencad.microstation.Layer(name, co=None, lw=None, lc=None)[source]

MicroStation level wrapper.

Sets the bylevel color, line-weight, and line-type to the values provided by the co, lw, and lc. The values default to “bylevel” which uses ustation’s default/current settings.

Parameters:
  • name (string OR Layer-like) – The name visible in ustation after the script is run. A tuple of level properties, or a Layer object may be passed as the first argument (name) in which case the new level is initialized with provided values.
  • co (int) – The color of the level.
  • lw (number) – The line-weight of the level.
  • lc (number) – The line-class (type) of the level.

CommandFile

class pygencad.microstation.CommandFile(filelike, setup=None, teardown=None)[source]

Wrapper for MicroStation script generation.

The ustation wrapper should have additional methods for setting ACS and placing cells, mirroring the autocad module’s methods.

Parameters:
  • filelike (filelike) – An object with a write method.
  • setup (string OR callable) – Commands to include at the beginning of the script.
  • teardown (string OR callable) – Commands to include at the end of the script.
class Layer(name, co=None, lw=None, lc=None)

A local binding to the layer class

CommandFile.setup()[source]

Write default configuration info and run user provided setup func.

CommandFile.teardown()[source]

Write cleanup commands and run user provided teardown func.

CommandFile.point(p, reset=False, mode='xy', write=True)[source]

Convert an iterable into a point of the correct format.

The mode argument defaults to ‘xy=’ absolute mode. Other modes can be specified, but currently no additional processing is performed.

Parameters:
  • p – The point to be converted.
  • reset (boolean) – Optionally append the result of calling the reset method after writing the point.
  • mode (string) – Optional point output mode.
  • write (boolean) – Flag for writing output to script file.
Returns:

The point converted to a string.

CommandFile.points(ps, reset=False, mode='xy')[source]

Calls CommandFile.point on each member of an iterable.

Parameters:
  • ps – The points to be converted.
  • reset (boolean) – Optionally append the result of calling the reset method after writing all points.
  • mode (string) – Optional point output mode (unimplemented in base class).
CommandFile.reset(write=True)[source]

Write a reset command.

Parameters:write (boolean) – Flag for writing output to script file.
Returns:The command that output by reset as a string.
CommandFile.line(ps, reset=False, mode='xy')[source]

Write the place line command.

Parameters:
  • ps – The end points of the lines.
  • reset (boolean) – Optionally append the result of calling the reset method after writing all points.
  • mode (string) – Optional point output mode.
CommandFile.polyline(ps, close=True, reset=False, mode='xy')[source]

Write the smartline command.

Support could be added in the future for drawing arc segments or other special smartline settings.

Parameters:
  • ps – The ordered points forming the line.
  • close (boolean) – Flag indicating whether the line should end where it started.
  • reset (boolean) – Optionally append the result of calling the reset method after writing all points.
  • mode (string) – Optional point output mode.
CommandFile.circle(center, radius)[source]

Write the place circle command.

The circle method uses “dx=” mode for setting the radius.

Parameters:
  • center (iterable) – Point in a format passable to CommandFile.point.
  • radius (number) – Radius of the circle.
CommandFile.text(text, p, height=None, ang=None, style=None)[source]

Write the place text command.

If a height is provided the text width is set to match. The “choose element” command is written after the text to avoid placing the same text accidentally.

Parameters:
  • text (string) – The text to place.
  • p (iterable) – The insertion point in a format passable to CommandFile.point.
  • height (number) – How tall the text lines are.
  • ang (number) – The angle of the text in degrees.
  • style (string) – The text style to activate before placing the text.
CommandFile.store(name, *els)[source]

Store provided elements to be manipulated later.

In MicroStation named groups are used. If ‘l’ is in els only the last element is added to the set. Otherwise the elements in the provided groups are joined into the named group.

Parameters:
  • name (MicroStation quick select group) – The id used to store the selection.
  • *els (previous group names, or 'l' for last element) – The elements to store.
Raises:

ValueError if no elements are provided.

Returns:

An element group passable to move/copy/rotate/scale methods.

CommandFile.move(els, base=(0, 0), dest=(0, 0))[source]

Transform the provided elements from base to dest.

Parameters:
  • els (iterable, selection name, 'l' for last, or 'p' for previous) – The elements to move.
  • base (iterable) – The base point to transform from.
  • dest (iterable) – The dest point to transform to.
Returns:

Reference to modified objects.

CommandFile.copy(els, base=(0, 0), dest=(0, 0))[source]

Transform duplicates of the provided elements from base to dest.

Parameters:
  • els (iterable, selection name, 'l' for last, or 'p' for previous) – The elements to copy.
  • base (iterable) – The base point to transform from.
  • dest (iterable) – The dest point to transform to.
Returns:

Reference to duplicated objects.

CommandFile.rotate(els, base=(0, 0), ang=0)[source]

Rotate the provided elements from base by angle.

Parameters:
  • els (iterable, selection name, 'l' for last, or 'p' for previous) – The elements to rotate.
  • base (iterable) – The base point to transform from.
  • ang (number) – The angle to rotate in degrees (counter-clockwise).
Returns:

Reference to modified objects.

CommandFile.scale(els, base=(0, 0), scale=1)[source]

Scale the provided elements from base by scale.

Parameters:
  • els (iterable, selection name, 'l' for last, or 'p' for previous) – The elements to scale.
  • base (iterable) – The base point to transform from.
  • scale (number) – The scale factor to apply.
Returns:

Reference to modified objects.

CommandFile.erase(els)[source]

Remove the indicated elements.

Parameters:els (iterable, selection name, 'l' for last, or 'p' for previous) – The elements to remove.
CommandFile.cmd(command, *args)[source]

Write an arbitrary script command.

If the command string contains format expressions, and additional arguments were passed, the command string will be formated using the additional arguments.

>>> from pygencad.commandfile import CommandFile
>>> from StringIO import StringIO
>>> f = StringIO()
>>> with CommandFile(f) as script:
...     script.cmd("command with data: {:0.5f}", 355/113.0)
... 
'l'
>>> print f.getvalue()
command with data: 3.14159
Parameters:
  • command (string) – The command to be written to the script.
  • *args – Values to be formated by the command string.
Returns:

Some reference to any object created.

CommandFile.layer(*args, **kwds)[source]

Script layer context manager.

Activates the provided layer settings, and resets layer state on exit.

>>> from pygencad.commandfile import CommandFile
>>> from StringIO import StringIO
>>> f = StringIO()
>>> with CommandFile(f) as script:
...     with script.layer('layer'):
...         script.cmd("test")
... 
'l'
>>> print f.getvalue() 
Layer:
    lv=layer
    co=None
    lw=None
    lc=None
test
Parameters:
  • *args – Valid Layer arguments.
  • **kwargs – Valid Layer arguments.
CommandFile.pop_layer()[source]

Restores active layer prior to last set_layer call.

CommandFile.set_layer(layer, *args, **kwargs)[source]

Activates the provided layer object or description.

Parameters:
  • layer – A layer object, or a valid Layer name argument.
  • *args – Valid Layer arguments.
  • **kwargs – Valid Layer arguments.