Asegúrese de que hay una capa de estructura de parcela presentes en el mapa y ha iniciado la edición. agregue el siguiente assemlbies a su proyecto, si no lo han hecho ya.
- ESRI.ArcGIS.CadastralUI
- ESRI.ArcGIS.GeoSurvey
- ESRI.ArcGIS.GeoDatabaseExtensions
El uso de esta Clase y sus métodos para crear una estructura de parcela de un paquete característica. Este método se ha probado, sin embargo si tu encontrado algún error, que me haga saber.
//@FaridCher at GIS.StackExchange
//can be copied and/or distributed without the express permission
using ESRI.ArcGIS.CadastralUI;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.GeoDatabaseExtensions;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GeoSurvey;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace MyParcelFabric
{
class CreateParcelFabric_FromPolygonSelection
{
protected override void OnClick()
{
var enums = ArcMap.Document.ActiveView.FocusMap.FeatureSelection as IEnumFeature;
IFeature feat = enums.Next();
if (feat == null)
{
return;
}
CreateParcelFabric(feat);
}
private void CreateParcelFabric(IFeature polygonFeature)
{
try
{
UID pUID = new UIDClass();
pUID.Value = "{114D685F-99B7-4B63-B09F-6D1A41A4DDC1}";
ICadastralExtensionManager2 pCadExtMan = (ICadastralExtensionManager2)ArcMap.Application.FindExtensionByCLSID(pUID);
ICadastralEditor pCadEd = (ICadastralEditor)ArcMap.Application.FindExtensionByCLSID(pUID);
IParcelEditManager pParcEditorMan = (IParcelEditManager)pCadEd;
IEditor pEd = (IEditor)ArcMap.Application.FindExtensionByName("esri object editor");
if (pEd.EditState == esriEditState.esriStateNotEditing)
{
MessageBox.Show("Please start editing and try again.");
return;
}
ICadastralPacketManager pCadPacketMan = (ICadastralPacketManager)pCadEd;
bool bStartedWithPacketOpen = pCadPacketMan.PacketOpen;
if (!bStartedWithPacketOpen)
pEd.StartOperation();
//Start map edit session
ICadastralMapEdit pCadMapEdit = (ICadastralMapEdit)pCadEd;
pCadMapEdit.StartMapEdit(esriMapEditType.esriMEEmpty, "NewParcel", false);
//Get job packet
ICadastralPacket pCadaPacket = pCadPacketMan.JobPacket;
//Create Plan (new)
string sPlanName = "My New Plan";
IGSPlan pGSPlan = null;
pGSPlan = new GSPlanClass();
//set values
pGSPlan.Accuracy = 4;
pGSPlan.Name = sPlanName;
//Add the plan to the job packet
ICadastralPlan pCadaPlan = (ICadastralPlan)pCadaPacket;
pCadaPlan.AddPlan(pGSPlan);
//Create Parcel
ICadastralParcel pCadaParcel = (ICadastralParcel)pCadaPacket;
IGSParcel pNewGSParcel = new GSParcelClass();
//Make sure that any extended attributes on the parcel have their default values set
IGSAttributes pGSAttributes = (IGSAttributes)pNewGSParcel;
if (pGSAttributes != null)
{
ICadastralObjectSetup pCadaObjSetup = (ICadastralObjectSetup)pParcEditorMan;
pCadaObjSetup.AddExtendedAttributes(pGSAttributes);
pCadaObjSetup.SetDefaultValues(pGSAttributes);
}
//Add the parcel to the packet. (do this before addlines)
//This will enable us to Acquire the parcel ID,
//Having the parcel attached to the packet allows InsertLine to function.
pCadaParcel.AddParcel(pNewGSParcel);
pNewGSParcel.Lot = "NewParcel";
pNewGSParcel.Type = 7;
//Set Plan (created above)
IGSPlan thePlan = pCadaPlan.GetPlan(sPlanName);
pNewGSParcel.Plan = thePlan;
ICadastralPoints pCadaPoints = (ICadastralPoints)pCadaPacket;
IMetricUnitConverter pMetricUnitConv = (IMetricUnitConverter)pCadEd;
ISegmentCollection segments = polygonFeature.ShapeCopy as ISegmentCollection;
//first segment point of polygon as the start of ParFab
var pPt1 = segments.get_Segment(0).FromPoint;
IZAware pZAw = (IZAware)pPt1;
pZAw.ZAware = true;
pPt1.Z = 0; //defaulting to 0
//Convert the point into metric units, and get a new (in-mem) point id
IGSPoint pGSPointFrom = pMetricUnitConv.SetGSPoint(pPt1);
pCadaPoints.AddPoint(pGSPointFrom);
int iID_First_Last = pGSPointFrom.Id;
int iID1 = iID_First_Last;
int iID2 = -1;
int index = 0;
for (int i = 0; i < segments.SegmentCount; i++)
{
ISegment seg = segments.get_Segment(i);
double bearingRad = getBearing(seg.FromPoint, seg.ToPoint);
bool computeToPoint = true;
if (i == segments.SegmentCount - 1)
computeToPoint = false;
IGSLine pGSLine = CreateGSLine(pMetricUnitConv, pCadaPoints, ref pPt1,
iID1, bearingRad, seg.Length, 0, -1, -1, -1, computeToPoint, out iID2);
iID1 = iID2;
iID2 = -1;
if (i == segments.SegmentCount - 1)
pGSLine.ToPoint = iID_First_Last;
pNewGSParcel.InsertLine(++index, pGSLine);
}
//Add radial lines for circular curves
pNewGSParcel.AddRadialLines();
//then set join=true on the parcel.
pNewGSParcel.Joined = true;
//let the packet know that a change has been made
pCadPacketMan.SetPacketModified(true);
try
{
pCadMapEdit.StopMapEdit(true);
}
catch
{
if (!bStartedWithPacketOpen)
pEd.AbortOperation();
return;
}
if (!bStartedWithPacketOpen)
pEd.StopOperation("New Parcel Fabric");
pCadPacketMan.PartialRefresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private IGSLine CreateGSLine(IMetricUnitConverter MetricConversion, ICadastralPoints CadastralPoints,
ref IPoint FromPointInToPointOut, int FromPointID, double Direction, double Distance,
double Radius, int Accuracy, int UserLineType, int Category, bool ComputeToPoint, out int ToPointID)
{
//In this function, Radius == 0 means a straight line
//If the radius is >0 or <0 then the line is a circular curve with Distance as the chord length
//for curves Bearing means chord bearing
//negative radius means a curve to the left, positive radius curve to the right
//for no Accuracy, no Type, or no Category pass in -1
//Bearing is in north azimuth radians
IGSLine pLine = new GSLineClass();
pLine.Bearing = Direction; //direction is in radians north azimuth
double dConvertedDistance = 0;
MetricConversion.ConvertDistance(esriCadastralUnitConversionType.esriCUCToMetric, Distance, ref dConvertedDistance);
pLine.Distance = dConvertedDistance; //needs to be in meters;
if (Math.Abs(Radius) > 0)
{
MetricConversion.ConvertDistance(esriCadastralUnitConversionType.esriCUCToMetric, Radius, ref dConvertedDistance);
pLine.Radius = dConvertedDistance; //needs to be in meters;
}
pLine.FromPoint = FromPointID;
pLine.ToPoint = -1;
if (Accuracy > -1)
pLine.Accuracy = Accuracy;
if (UserLineType > -1)
pLine.LineType = UserLineType;
if (Category > -1)
pLine.Category = (ESRI.ArcGIS.GeoDatabaseExtensions.esriCadastralLineCategory)Category;
//Make sure that any extended attributes on the line have their default values set
IGSAttributes pGSAttributes = (IGSAttributes)pLine;
if (pGSAttributes != null)
{
ICadastralObjectSetup pCadaObjSetup = (ICadastralObjectSetup)MetricConversion; //QI
pCadaObjSetup.AddExtendedAttributes(pGSAttributes);
pCadaObjSetup.SetDefaultValues(pGSAttributes);
}
//Compute the new end point for the line.
//FromPointInToPointOut is in units of the map projection.
ICurve pCurv = MetricConversion.GetSurveyedLine(pLine, CadastralPoints, false, FromPointInToPointOut);
//pCurv is also in the units of the map projection. Convert the end point to metric units.
FromPointInToPointOut = pCurv.ToPoint;//pass the new To point back out
FromPointInToPointOut.Z = 0;
IGSPoint pGSPointTo = MetricConversion.SetGSPoint(FromPointInToPointOut);
if (ComputeToPoint)
{
CadastralPoints.AddPoint(pGSPointTo);
pLine.ToPoint = pGSPointTo.Id;
ToPointID = pLine.ToPoint;
}
else
ToPointID = -1;
if (pCurv is ICircularArc)
{
ICircularArc pCircArc = (ICircularArc)pCurv;
IPoint pCtrPt = pCircArc.CenterPoint;
IZAware pZAw = (IZAware)pCtrPt;
pZAw.ZAware = true;
pCtrPt.Z = 0;
IGSPoint pGSCtrPt = MetricConversion.SetGSPoint(pCtrPt);
CadastralPoints.AddPoint(pGSCtrPt);
pLine.CenterPoint = pGSCtrPt.Id;
}
return pLine;
}
private double getBearing(IPoint fromPoint, IPoint toPoint)
{
ILine line = new LineClass();
line.PutCoords(fromPoint, toPoint);
double angle = line.Angle;
return Math.PI / 2 - angle;
}
}
}