3DPolys from 2DPolys with elev in record

From Vectorlab
Jump to: navigation, search


I wrote this procedure to process a layer of 2D polygons imported from a shapefile (.shp). Each polygon had the polygon's elevation in an attached record. This procedure reads the elevation value to create a 3D polygon at the z height. This procedure could be made more useful; The procedure could ask for the record and field instead of constants. Also, the 3D polys are placed onto a new layer, but the 2D polys could be separated from the 3D polys more elegantly. By Frank Brault


PROCEDURE convertShapeData;
CONST
	kRecordName = '1''contour';
	kFieldName = 'CONTOUR';
	kTargetLayerName = '3D polygons';
VAR
	gSourceLayerName : STRING;

    FUNCTION ClosedPoly(hPoly : HANDLE) : BOOLEAN;
    {** Written by Julian Carr based on work by Tom Urie.}
    VAR
        x0, y0, x1, y1, x2, y2, Perim : Real;
        j : Integer;
    BEGIN
	Perim := 0;
	FOR j := 1 TO GetVertNum(hPoly) DO
	BEGIN
            GetPolyPt(hPoly, j, x2, y2);
            IF j = 1 THEN BEGIN
                x0 := x2;
                y0 := y2;
            END ELSE
                Perim := Perim + Distance(x2, y2, x1, y1);

            x1 := x2;
            y1 := y2;
	END;

	Perim := Perim + Distance(x1, y1, x0, y0);
	ClosedPoly := Num2Str(5, Perim) = Num2Str(5, HPerim(hPoly));
    END;

    PROCEDURE Convert2Polys(sourceHandle : HANDLE);
    VAR
        elev, verX, verY, verX2, verY2 : REAL;
        i, totVertex : INTEGER;
        newHandle : HANDLE;
    BEGIN
        totVertex := GetVertNum(sourceHandle);
        Layer(kTargetLayerName);
        GetPolyPt(sourceHandle,1,verX,verY);
        GetPolyPt(sourceHandle,2,verX2,verY2);
        elev := Str2Num(GetRField(sourceHandle, kRecordName, kFieldName))* 12;
	
        {** create 3D poly and acquire handle }
        IF ClosedPoly(sourceHandle) THEN
            ClosePoly Else OpenPoly;

        Poly3D(verX, verY, elev, verX2, verY2, elev);
        newHandle := LNewObj;
        FOR i := 3 TO totVertex DO BEGIN
            GetPolyPt(sourceHandle, i, verX, verY);
            AddVertex3D(newHandle, verX, verY, elev);
        END;

        SetDSelect(newHandle);
        Layer(gSourceLayerName);
    END; {** of Convert2Polys procedure }

BEGIN
    gSourceLayerName := GetLName(ActLayer);
    ForEachObject(Convert2Polys, (T=POLY));
    SelectObj((T=POLY));
    RedrawAll;
END;
Run(convertShapeData);