NAME

CAD::Drawing::Template - Replace tags with text and geometry.

SYNOPSIS

my $bp = CAD::Drawing::Template->new();
$bp->load('my_template.dxf');
# set some values for the boiler-plate:
$bp->set_data(foo => 'value for foo');
my @parts = qw(E8955 Q4200);
$bp->set_vtable(parts => \@parts);
$bp->set_geom(birdseye => 'birdseye.dwg');
my $drw = $bp->done(pass => qr/^shipping/, die => 0);
$drw->save('output.dxf');

Input Templates

Input templates must be CAD::Drawing compatible files or objects. These are brought into the CAD::Drawing::Template object via load() or import() and searched for 'texts' items which match the formats listed below.

The tags may be on any layer in the drawing except 'comments' and 'fit' which are reserved names. The 'comments' layer is completely discarded, and the 'fit' layer must only contain rectangles (which are necessary for scaling calculations, but are also discarded.)

Tag Formats

The 'tags' are 'texts' entities (single-line text in dwg/dxf formats) which must begin and end with matching angle-brackets ('<' and '>'.) These text entities are sourced for their insertion point, text height, and name. Future versions of this module will support orientations, fonts, and options within the tag text itself.

In general, tags are formatted as <$type:$name>. Where $type is one of the types defined below and $name is the name of the tag (to be used in addressing it via the set_*() functions.

Tag names should adhere to the same rules as perl variable names:

1.  Alphanumeric characters (and underscores) only (a-z, A-Z, 0-9, _)
2.  Must start with a letter ("a2", not "2a", and not "_2")
3.  Case senSitive

The following tag types are supported. Examples show the text string that would be in the template.

data

A 'data' tag is replaced with a single scalar value.

Examples:

<data:department>
<data:item_code>
<data:A5>
vtable

A 'vtable' tag is replaced with a list of values, each one some distance below the previous, with the top line's insertion point at the tag's insertion point.

Examples:

<vtable:revision>
<vtable:part_list>
geo

Loads a drawing and fits it into a rectangle.

NOTE: The rectangle must be on a layer named 'fit' and contain the insertion point of the tag. Each <geo:name> tag must be within a rectangle on the 'fit' layer and each rectangle on the 'fit' layer must have exactly one <geo:name> tag inside of it. If this is not true, death ensues. These rectangles are removed from the drawing before output.

While a rectangle may contain two 'geo' tags, each tag must be contained in one rectangle (the innermost containing rectangle wins.)

Examples:

<geo:section>
<geo:isometric>
block

Loads a drawing to the insertion point.

Examples:

<block:north_arrow>
<block:scale>
function

A 'function' tag calls a perl function, and afterwards behaves like a data tag. There is no set_function() function, since this tag is supposed to be fully-automatic.

The function is assumed to be a member of a Perl module. If that module is not already loaded, it is require()'d within an eval() statement before the function is called. There is no provision for passing values to these functions. The function is called in a list context, and the results joined by spaces. Any errors encountered in calling the function will be croak()'d along with the function name.

If the module is contained under a non-standard path (one which is not included in @INC), it should be preceded by a directory path. This directory is then brought into @INC via the 'use lib' pragma.

Examples:

<function:date>                  # uses main::date()
<function:Date::Calc::Today>
<function:CAD::Calc::pi>
<function:my_perl_lib/Functions::foo>

AUTHOR

Eric L. Wilhelm <ewilhelm at cpan dot org>

http://scratchcomputing.com

COPYRIGHT

This module is copyright (C) 2004-2006 by Eric L. Wilhelm.

LICENSE

This module is distributed under the same terms as Perl. See the Perl source package for details.

You may use this software under one of the following licenses:

(1) GNU General Public License
  (found at http://www.gnu.org/copyleft/gpl.html)
(2) Artistic License
  (found at http://www.perl.com/pub/language/misc/Artistic.html)

Modifications

The source code of this module is made freely available and distributable under the GPL or Artistic License. Modifications to and use of this software must adhere to one of these licenses. Changes to the code should be noted as such and this notification (as well as the above copyright information) must remain intact on all copies of the code.

Additionally, while the author is actively developing this code, notification of any intended changes or extensions would be most helpful in avoiding repeated work for all parties involved. Please contact the author with any such development plans.

SEE ALSO

CAD::Drawing

Constructors

new

my $bp = CAD::Drawing::Template->new(%options);
Valid options
pass => [@list], # type:name strings only

clone

Duplicates the boiler-plate as a snapshot in time (useful to save effort in loops.)

my $bp2 = $bp->clone();

Template Handling

Getting template data in and finished data out.

load

$bp->load($filename);
# or:
$bp->load($drawing_object);

done

$drw = $bp->done(%options);

Options:

pass     - array ref of pass-able tags ("type:name" strings)
strict   - croak on tags not listed in pass
warnings - carp warnings
default  - "drop" or "pass" (default) action for un-passed tags

tag_list

$bp->tag_list();

Methods

These methods allow you to manipulate the template.

set_data

Replace the tag's text with a string.

$bp->set_data($name => $value);

# replace the tag <data:department> with the department's name:
$dep = 'Department of Redundancy Department';
$bp->set_data(department => $dep);

set_vtable

Remove the tag entity, and create a series of texts, each spaced slightly below the previous.

$bp->set_vtable($name => \@list);

# uses the tag:  <vtable:revision>
# create a table of revision notes:
my @rev = (
  '  1  Changed fonts for PHB',
  '  2  Changed fonts back (for same)',
  '  3  Removed all text',
  );
$bp->set_vtable(revision => \@rev);

set_geo

Load a drawing into the template, scaling it to fit within an enclosing rectangle.

$bp->set_geo($name => $filename);
# or:
$bp->set_geo($name => $drawing_object);

set_block

Identical to set_geo, except no scaling is performed.

$bp->set_block($name => $filename);
# or:
$bp->set_block($name => $drawing_object);

Guts

These methods are used internally.

find_tags

Grabs the addresses of all tags which match the regex m/^<.*>$/. Any which were are in the array @{$self->{pass}} are left untouched.

After finding all of the tags, execute any <function:*> tags which were found.

$bp->find_tags();

geo_match

Performs the rectangle-tag matching. Must be able to reduce each geo tag to an innermost enclosing rectangle or dies with much whining.

$bp->geo_match();

run_function

Runs the function $name (in a list context) and places it's results (joined with spaces) into the string at $addr.

$bp->run_function($name, $addr);

load_drawing

Loads a drawing from a filename or CAD::Drawing object and returns a CAD::Drawing object.

$drw = $bp->load_drawing($name => $filename);
# or:
$drw = $bp->load_drawing($name => $drawing_object);

Functions

Not object-oriented, and likely not exported.

parse_tag

Break a tag into type, name, and options. When (and if) options are supported within the tags, they will be handled here.

($type, $name, $options) = parse_tag($tag);