More scripts: Vector
Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# CreatePolyStyles.sml
# Sample script that sets up styles by attribute for vector polygons.
# Creates the styles in a style subobject and sets up the style assignment table.
# Sets display parameters for polygons to By Attribute using these styles.
# Designed for an input vector with an attribute table "PolyType" with
# classes A, B, and C in the PolyType.Type field.
# The input vector object is copied to a new vector object in the same Project File,
# and styles are set up for the copy.
# Requires TNTgis 2016 dated 4 March 2015 or later.
clear();
# get the input vector
class RVC_VECTOR PolyVecIn;
class RVC_OBJITEM vecInObjItem; # to be returned by DlgGetObject function
DlgGetObject("Select vector object:", "Vector", vecInObjItem, "ExistingOnly");
PolyVecIn.Open(vecInObjItem, "Read");
# make a copy of the input vector in the same Project File and open it
class RVC_OBJECT vectorParent; # the Project File
PolyVecIn.GetParent(vectorParent); # get the parent object of the input vector
class RVC_OBJITEM fileObjItem = vectorParent.GetObjItem(); # ObjItem for the project file
vectorParent.Open(fileObjItem, "Write"); # must open the file to write in order to copy the vector
class RVC_VECTOR PolyVecOut; # new vector with polygons to get the styles
class RVC_Descriptor vecOutDescript;
vecOutDescript.SetName("PolyVecOut");
vecOutDescript.SetDescription("Polygon vector with styles created by script");
# copy the vector
PolyVecIn.CopyTo(vectorParent, PolyVecOut, "none", vecOutDescript);
class RVC_ObjItem vecOutObjItem;
vecOutObjItem = PolyVecOut.GetObjItem(); # get the ObjItem for the copy
PolyVecOut.Open(vecOutObjItem, "Write"); # open it
PolyVecIn.Close(); # close the input vector
# set up stringlist of key attribute values
class STRINGLIST attList;
attList.AddToEnd("A");
attList.AddToEnd("B");
attList.AddToEnd("C");
##############
# set up the polygon styles for polygon attributes "A", "B", "C"
class COLOR colorA, colorB, colorC, colorBorder;
numeric range = 100;
colorA.SetRGB(100, 0, 0, range); # red
colorB.SetRGB(100, 100, 0, range); # yellow
colorC.SetRGB(0, 100, 0, range); # green
colorBorder.SetRGB(0, 0, 0, range); # black border
class POLYSTYLE polystyleA, polystyleB, polystyleC;
polystyleA.FillColor = colorA;
polystyleA.Fill = 1; # fill the polygon
polystyleA.BorderColor = colorBorder;
polystyleA.DrawBorder = 1;
polystyleA.UseLayoutScale = 1;
polystyleA.Width = 0.5;
polystyleB.FillColor = colorB;
polystyleB.Fill = 1;
polystyleB.BorderColor = colorBorder;
polystyleB.DrawBorder = 1;
polystyleB.UseLayoutScale = 1;
polystyleB.Width = 0.5;
polystyleC.FillColor = colorC;
polystyleC.Fill = 1;
polystyleC.BorderColor = colorBorder;
polystyleC.DrawBorder = 1;
polystyleC.UseLayoutScale = 1;
polystyleC.Width = 0.5;
print("Created polygon styles.");
####################################
# create the style subobject in the output vector
class RVC_DESCRIPTOR styleDescript; # name and description for the style object
styleDescript.SetName("My_Styles");
styleDescript.SetDescription("Styles set up by SML Script");
class RVC_OBJITEM styleObjItem; # create the ObjItem for the style object
styleObjItem.CreateNew(vecOutObjItem, "STYLE", styleDescript);
class RVC_STYLE myStyles; # style object
myStyles.Make(styleObjItem); # make the style subobject
myStyles.Open(styleObjItem, "Write"); # open it
# add the polygon styles to the style object
myStyles.AddStyle("styleA", polystyleA);
myStyles.AddStyle("styleB", polystyleB);
myStyles.AddStyle("styleC", polystyleC);
print("Created the style object and added the styles.");
# set up stringlist of style names in same order as the associated attribute
class STRINGLIST stylenameList;
stylenameList.AddToEnd("styleA");
stylenameList.AddToEnd("styleB");
stylenameList.AddToEnd("styleC");
####################################
# open the output vector's polygon database
class RVC_DBASE_POLYGON polyDB;
polyDB.OpenAsSubobject(PolyVecOut, "Write");
# open the "PolyType" polygon table and get the FIELDINFO for the "Type" field
class RVC_DESCRIPTOR tblDescriptor;
tblDescriptor.SetName("PolyType");
class RVC_DBTABLE typeTbl;
typeTbl.Open(polyDB, tblDescriptor, "Read");
#class RVC_DBFIELDINFO typeFieldInfo;
#typeTbl.GetFieldInfo("Type", typeFieldInfo);
#####################################
# make a style assignment table: uses special class for style tables
class RVC_DBTABLE_STYLE styleAssignmentTbl; # style table class instance
tblDescriptor.SetName("PolyStyleAssign"); # name for the table, used in the Make method
# set up parameters for making the style assignment table: table and field to relate to, and table usage
class RVC_DBTABLE_MAKEPARM_STYLE_ELEMENT styleTblMakeParms(typeTbl, "Type", "PolyStyle");
styleAssignmentTbl.Make(polyDB, tblDescriptor, styleTblMakeParms);
print("Created style assignment table.");
######################################
# open the style assignment table for writing and set the link to the style object
styleAssignmentTbl.Open(polyDB, tblDescriptor, "Write");
styleAssignmentTbl.SetStyleLink(myStyles);
#######################################
# add records to the style assignment table
class RVC_DBTABLE_RECORD styleRecord; # class structure for a record
styleRecord.AssignTable(styleAssignmentTbl); # initialize record for the table
class RVC_RECORDNUM recordNum; # class to hold record number
numeric i; # counter
# loop through stringlists with attribute values and style names to populate records
for i = 0 to stylenameList.GetNumItems() - 1
{
# printf("i = %d, attribute = %s, style = %s\n", i, attList[i], stylenameList[i]);
styleRecord.SetValue("Type", attList[i]); # set attribute value
styleRecord.SetValue("_StyleName_", stylenameList[i]); # set style name
styleRecord.SetValue("_StyleIndex_", i);
styleRecord.SetValue("_DrawFlag_", 1);
styleAssignmentTbl.AddRecord(styleRecord, recordNum);
printf("New assignment table record number = %d\n", recordNum.Number);
}
# close the style assignment table and polygon database
styleAssignmentTbl.Close();
polyDB.Close();
#########################################################
# Set display parameters for the vector to use the polygon styles.
# This requires setting up a virtual display group and adding the output vector as
# a virtual layer.
print("Setting display parameters.")
class GRE_GROUP group; # create virtual display group
group = GroupCreate();
class GRE_LAYER_VECTOR vecLayer;
vecLayer = GroupQuickAddVectorVar( group, PolyVecOut); # add the output vector to the group
# set polygon style mode to ByAttribute
vecLayer.Poly.StyleMode = "ByAttribute";
# in order to set the style assignment table to use for display need to get a legacy DBTABLEINFO
# class instance for the table.
class DATABASE polygonDatabase;
polygonDatabase = OpenVectorPolyDatabase(PolyVecOut);
class DBTABLEINFO styleAssignmentTblInfo;
styleAssignmentTblInfo = DatabaseGetTableInfo(polygonDatabase, "PolyStyleAssign");
# set style assignment table to use
vecLayer.Poly.Table = styleAssignmentTblInfo;
# save the display parameters
vecLayer.SaveDispParmSubobject();
CloseDatabase(polygonDatabase);
PolyVecOut.Close();
print("Done.");