More scripts: C A D
Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# CAD.SML
# Sample script for tutorial "Writing Scripts with SML".
# Examples of CAD functions.
# revised and updated 9 October 2009 to use RVC object classes and RECT3D class
clear();
# Get input raster and find its geographic extents in map
# coordinates defined by its last-used georeference object.
class RVC_RASTER R;
GetInputRaster(R);
# get georeference and coordinate reference system for input raster
class RVC_GEOREFERENCE rGeoref;
R.GetDefaultGeoref(rGeoref);
# get extents of the input raster in object (line and column) coordinates
class RECT3D extents;
R.GetExtents(extents);
printf("Raster CRS = %s\n", rGeoref.GetCoordRefSys().Name);
# get coordinate transformation from raster object to map coordinates from the georeference
class TRANS2D_MAPGEN rTransObjToMap;
rGeoref.GetTransParm(rTransObjToMap, 0, rGeoref.GetCalibModel() );
# convert raster extents RECT3D to map coordinates
extents = rTransObjToMap.ConvertRectFwd(extents);
printf("Minimum extents of raster: MinX = %.1f, MinY = %.1f\n", extents.x1, extents.y1);
printf("Maximum extents of raster: MaxX = %.1f, MaxY = %.1f\n", extents.X2, extents.y2);
# print the center point map coordinates of the input raster
class POINT2D ctr = extents.center;
printf("Map X of raster center = %.1f\n", ctr.x);
printf("Map Y of raster center = %.1f\n", ctr.y);
# Get new output CAD object and create implied georeference
# using parameters from input raster.
class RVC_CAD CADout;
GetOutputCAD(CADout);
# set name and description for a georeference subobject of the vector
class RVC_DESCRIPTOR cgDescript;
cgDescript.SetName("Georeference");
cgDescript.SetDescription( sprintf("Implied Georeference to %s", rGeoref.GetCoordRefSys().Name) );
# Create georeference for CAD object set to same CRS as the input raster;
# set it to be implied georeference.
class RVC_GEOREFERENCE georefC;
georefC.SetCoordRefSys(rGeoref.GetCoordRefSys() );
georefC.SetImplied();
georefC.Make(CADout, cgDescript);
# Add circle to CAD centered at raster center and
# tangent to closest extents boundary.
numeric radius;
radius = SetMin(extents.GetWidth() / 2, extents.GetHeight() / 2);
CADWriteCircle(CADout, 1, ctr.x, ctr.y, radius);
# Add line to CAD from center to edge of circle at -45 degree angle.
numeric addX, addY;
addX = radius * cosd(-45);
addY = radius * sind(-45);
array numeric xpoints[2]; # arrays for line vertex coordinates.
array numeric ypoints[2];
xpoints[1] = ctr.x; ypoints[1] = ctr.y;
xpoints[2] = ctr.x + addX; ypoints[2] = ctr.y + addY;
CADWriteLine(CADout, 1, 2, xpoints, ypoints);
# Add box to CAD with lower left corner at center
# of circle, 2000 m X 2000 m.
CADWriteBox(CADout, 1, ctr.x, ctr.y, ctr.x + 2000, ctr.y + 2000);
# Add identical box rotated 180 degrees.
CADWriteBox(CADout, 1, ctr.x, ctr.y, ctr.x + 2000, ctr.y + 2000, 180);
# Read coordinates of lower left and upper right corners
# of box and print to console.
numeric lastelem;
numeric lowerleftX, lowerleftY, upperrightX, upperrightY, rot;
lastelem = CADNumElements(CADout,1); # number of last-added element.
CADReadBox(CADout, 1, lastelem, lowerleftX, lowerleftY, upperrightX, upperrightY, rot);
print("Coordinates of rotated box:");
print("Lower left corner X:" ,lowerleftX);
print("Lower left corner Y: ",lowerleftY);
print("Upper right corner X: ",upperrightX);
print("Upper right corner Y: ",upperrightY);
CloseCAD(CADout);