commandfile

Base classes for implementing drawing backend wrappers.

The base CommandFile class methods implement AutoCAD like outputs, and some are used un-modified in the autocad.CommandFile implementation.

classes

Layer

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

Object representing a grouping and styling context.

Layer like objects are expected to support at least naming, color, line weight, and line type. All properties except name are expected to be optional.

>>> from pygencad.commandfile import Layer
>>> l = Layer("test")
>>> l
<pygencad.commandfile.Layer object at 0x...>
>>> print l 
Layer:
    lv=test
    co=None
    lw=None
    lc=None
Parameters:
  • name (string OR Layer-like) – The name visible in the backend after the script is run. A tuple of layer properties, or a Layer object may be passed as the first argument (name) in which case the new layer is initialized with provided values.
  • co – The color of the layer.
  • lw – The line-weight of the layer.
  • lc – The line-class (type) of the layer.

Store properties and setup template.

__str__()[source]

Render this layer as a string.

The __str__ method is used to output the commands necessary to set the Layer objects properties active in the script context.

CommandFile

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

Script generation interface.

Provides convenience methods for issuing common commands, managing script context, and for issuing arbitrary commands.

>>> from pygencad.commandfile import CommandFile
>>> from StringIO import StringIO
>>> f = StringIO()
>>> with CommandFile(f) as script:
...     script.cmd("test")
... 
'l'
>>> print f.getvalue()
test
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 backend specific Layer class

CommandFile.setup()[source]

Write default configuration info and run user provided setup func.

Called automatically by CommandFile context manager. Writes default setup code, then adds any user provided setup. If the user setup is callable the return value is written to the script, otherwise the user setup is written as a string.

CommandFile.teardown()[source]

Write cleanup commands and run user provided teardown func.

Called automatically by CommandFile context manager. Writesany user provided teardown, then writes default teardown code. If the user teardown is callable the return value is written to the script, otherwise the user teardown is written as a string.

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.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.
CommandFile.pop_layer()[source]

Restores active layer prior to last set_layer call.

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.point(p, reset=False, mode=None, write=True)[source]

Convert an iterable into a point of the correct format.

This method should be overridden in sub-classes.

>>> from pygencad.commandfile import CommandFile
>>> from StringIO import StringIO
>>> f = StringIO()
>>> point = (1, 2, 3)
>>> with CommandFile(f) as script:
...     script.point(point)
...     script.point(point, reset=True)
...     script.point(point, mode='special')
...     p = script.point(point, write=False)
...     script.cmd("Here is the point: {}", p)
... 
'1,2,3'
'1,2,3\n'
'1,2,3'
'l'
>>> print f.getvalue()
1,2,3
1,2,3

1,2,3
Here is the point: 1,2,3
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 (unimplemented in base class).
  • write (boolean) – Flag for writing output to script file.
Returns:

The point converted to a string.

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

Calls CommandFile.point on each member of an iterable.

This method may need to be overridden in sub-classes.

>>> from pygencad.commandfile import CommandFile
>>> from StringIO import StringIO
>>> f = StringIO()
>>> with CommandFile(f) as script:
...     script.points((
...         (1, 2),
...         (3, 4),
...         (5, 6),
...     ))
... 
>>> print f.getvalue()
1,2
3,4
5,6
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 the sequence required to exit the current command.

This method may need to be overridden in sub-classes.

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=None)[source]

Write a line segment command for each pair of points provided

This method should be overridden in sub-classes. The output command for the line method is expected to produce a set of disconnected line segments. See the polyline command for outputting connected “line strings”.

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 unimplemented in base class.
Returns:

Some reference to any object created.

CommandFile.polyline(ps, close=True, reset=False, mode=None)[source]

Write a string of lines connected at each of the provided points.

This method should be overridden in sub-classes. The output command for the polyline method is expected to produce a single object connecting all the provided points in order. The object produced is expected to be “closable”. Otherwise the method should ensure that the last point is equal to the first to close the polyline.

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 unimplemented in base class.
Returns:

Some reference to any object created.

CommandFile.circle(center, radius)[source]

Write a circle command.

This method should be overridden in sub-classes.

Parameters:
  • center (iterable) – Point in a format passable to CommandFile.point.
  • radius (number) – Radius of the circle.
Returns:

Some reference to any object created.

CommandFile.text(text, p, height=1.0)[source]

Write a text placement command.

This method should be overridden in sub-classes.

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.
Returns:

Some reference to any object created.

CommandFile.store(name, *els)[source]

Store provided elements to be manipulated later.

This method should be overridden in sub-classes.

This method provides a uniform interface for grouping objects regardless of backend. This is required for the move/copy/rotate/scale methods to be of any practical use. Each backend’s store method should transparently handle combining elements and groups.

Parameters:
  • name (backend specific) – The id used to store the selection.
  • *els (backend specific) – 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 (backend specific) – The elements to move.
  • base (iterable) – The base point to transform from.
  • dest (iterable) – The dest point to transform to.
Returns:

Some 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 (backend specific) – The elements to copy.
  • base (iterable) – The base point to transform from.
  • dest (iterable) – The dest point to transform to.
Returns:

Some reference to duplicated objects.

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

Rotate the provided elements from base by angle.

Parameters:
  • els (backend specific) – The elements to rotate.
  • base (iterable) – The base point to transform from.
  • ang (number) – The angle to rotate in degrees (counter-clockwise).
Returns:

Some reference to modified objects.

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

Scale the provided elements from base by scale.

Remember that scaling an axis by -1 is equivalent to mirroring.

Parameters:
  • els (backend specific) – The elements to scale.
  • base (iterable) – The base point to transform from.
  • scale (number) – The scale factor to apply.
Returns:

Some reference to modified objects.

CommandFile.erase(els)[source]

Remove the indicated elements.

Parameters:els (backend specific) – The elements to remove.