Friday, January 2, 2009

Creating 3D Geometry

To create 3D geometry is pretty straight forward using ArcObjects. Any time you create a piece of geometry it is at first only 2 dimensional. Once you have a 2D geometry the next step is to make it Z Aware. Once accomplished you set the Z value. For example, to make a 3D point, you first create the point and then via casting (or Query Interface in COM speak) you then make the geometry Z Aware. Here is an example:

IPoint pPoint1 = new PointClass();

pPoint1.PutCoords (x, y);

((IZAware)pPoint1).ZAware = true;

pPoint1.Z = 1000;

From this simple example more complex geometry can be created. To create a line using two points is quite straight forward. Create two points, make them Z-Aware and assign the Z values, and create a new line and then construct the new line using the two points. You don’t need to make the line Z-Aware because it will inherit the Z values of the points. Here is another example:

IPoint pPoint1 = new PointClass();

pPoint1.PutCoords (x, y);

((IZAware)pPoint1).ZAware = true;

pPoint1.Z = 1000;

IPoint pPoint2 = new PointClass();

pPoint2.PutCoords (x, y);

((IZAware)pPoint2).ZAware = true;

pPoint2.Z = 1000;

ILine = pLine = new LineClass();

pLine.PutCoords(pPoint1, pPoint2);

So far so good. You can now use that line in many ways. However, the most important thing to know is that in order to create more complex geometry you must now make the more complex geometry also Z-Aware. Let’s create a polygon that is Z-Aware. In order to do that you must first create segments from the lines, and then make the segments Z-Aware, then create a ring, then create a polygon and make it Z-Aware and lastly add the Z-Aware segments to the ring. Here is a simple square polygon:

IPoint point1 = new PointClass();

IPoint point2 = new PointClass();

IPoint point3 = new PointClass();

IPoint point4 = new PointClass();

point1.PutCoords(1, 1);

point2.PutCoords(1, 2);

point3.PutCoords(2, 2);

point4.PutCoords(2, 1);

ILine line1 = new LineClass();

ILine line2 = new LineClass();

ILine line3 = new LineClass();

ILine line4 = new LineClass();

line1.PutCoords(point1, point2);

line2.PutCoords(point2, point3);

line3.PutCoords(point3, point4);

line4.PutCoords(point4, point1);

ISegmentZ line1SegmentZ = line1 as ISegmentZ;

line1SegmentZ.SetZs(1000, 1000);

ISegmentZ line2SegmentZ = line2 as ISegmentZ;

line2SegmentZ.SetZs(1000, 1000);

ISegmentZ line3SegmentZ = line3 as ISegmentZ;

line3SegmentZ.SetZs(1000, 1000);

ISegmentZ line4SegmentZ = line4 as ISegmentZ;

line4SegmentZ.SetZs(1000, 1000);

IPolygon polygon = new PolygonClass();

((IZAware)polygon).ZAware = true;

ISegmentCollection ring = new RingClass();

object missing = Type.Missing;

ring.AddSegment((ISegment)line1SegmentZ, ref missing, ref missing);

ring.AddSegment((ISegment)line2SegmentZ, ref missing, ref missing);

ring.AddSegment((ISegment)line3SegmentZ, ref missing, ref missing);

ring.AddSegment((ISegment)line4SegmentZ, ref missing, ref missing);

IGeometryCollection geometryCollection = polygon as IGeometryCollection;

geometryCollection.AddGeometry((IGeometry)ring, ref missing, ref missing);



That’s it! We now have a complete polygon. In this case the polygon will float at 1000 units. Notice that at the end the ring was added to a geometry collection, the polygon was closed and lastly the from/to points of the polygon were preserved because the precise order of the points was established when the lines were created. From this point, this polygon could be drawn in ArcMap, ArcScene or ArcGlobe or stored in a geodatabase if the featureclass is also Z-Aware. There are of course other ways to create a polygon too like just using a point collection.

No comments:

Post a Comment