Skip to content

ProSnippets ParcelFabric

uma2526 edited this page Feb 5, 2020 · 11 revisions
Language:              C#  
Subject:               ParcelFabric  
Contributor:           ArcGIS Pro SDK Team <[email protected]>  
Organization:          esri, http://www.esri.com  
Date:                  1/16/2020  
ArcGIS Pro:            2.5  
Visual Studio:         2017, 2019  
.NET Target Framework: 4.8  

Get the active record

await QueuedTask.Run(async () =>
{
  var layers = MapView.Active.Map.GetLayersAsFlattenedList();
  var myParcelFabricLayer = layers.FirstOrDefault(l => l is ParcelLayer) as ParcelLayer;
  var theActiveRecord = myParcelFabricLayer.GetActiveRecord();
  if (theActiveRecord == null)
  {
    System.Windows.MessageBox.Show("There is no Active Record. Please set the active record and try again.",
              "Getting the Active Record");
    return;
  }
});

Set the active record

await QueuedTask.Run(async () =>
{
  var layers = MapView.Active.Map.GetLayersAsFlattenedList();
  var myParcelFabricLayer = layers.FirstOrDefault(l => l is ParcelLayer) as ParcelLayer;
  var recordsLayer = layers.FirstOrDefault(l => l.Name == "Records" && l is FeatureLayer);
  string sExistingRecord = "MyRecordName";
  
  var pFeatClass = (recordsLayer as FeatureLayer).GetFeatureClass();
  QueryFilter queryFilter = new QueryFilter
  {
    WhereClause = "Name = '" + sExistingRecord + "'"
  };
  Guid guid = new Guid();
  long lOID = -1;

  using (RowCursor rowCursor = pFeatClass.Search(queryFilter, false))
  {
    while (rowCursor.MoveNext())
    {
      using (Row row = rowCursor.Current)
      {
        guid = row.GetGlobalID();
        long oid = row.GetObjectID();
      }
    }
  }

  var parcelRecord=new ParcelRecord(myParcelFabricLayer.Map, sExistingRecord, guid, lOID);
  await myParcelFabricLayer.SetActiveRecord(parcelRecord);
});

Create a new record

await QueuedTask.Run(async () =>
{
  Dictionary<string, object> RecordAttributes = new Dictionary<string, object>();
  var layers = MapView.Active.Map.GetLayersAsFlattenedList();
  var recordsLayer = layers.FirstOrDefault(l => l.Name == "Records" && l is FeatureLayer);
  var myParcelFabricLayer = layers.FirstOrDefault(l => l is ParcelLayer) as ParcelLayer;
  var spatRef = recordsLayer.Map.SpatialReference;

  var editOper = new EditOperation()
  {
    Name = "Create Parcel Fabric Record",
    ProgressMessage = "Create Parcel Fabric Record...",
    ShowModalMessageAfterFailure = true,
    SelectNewFeatures = false,
    SelectModifiedFeatures = false
  };

  string sNewRecordName = "myNewRecord";
  Polygon newPolygon = null;
  newPolygon = PolygonBuilder.CreatePolygon(spatRef);
  if (newPolygon != null)
  {
    RecordAttributes.Add("Name", sNewRecordName);
    var editRowToken = editOper.CreateEx(recordsLayer, newPolygon, RecordAttributes);
    RecordAttributes.Clear();
    if (!await editOper.ExecuteAsync())
      return;

    //Default Guid
    var defGuid = new Guid("dddddddd-dddd-dddd-dddd-dddddddddddd");
    var defOID = -1;
    var guid = editRowToken.GlobalID.HasValue ? editRowToken.GlobalID.Value : defGuid;
    var lOid = editRowToken.ObjectID.HasValue ? editRowToken.ObjectID.Value : defOID;

    if (guid == defGuid | lOid == defOID)
      return;

    ParcelRecord parcelRecord = new ParcelRecord(myParcelFabricLayer.Map, sNewRecordName, guid, lOid);
    await myParcelFabricLayer.SetActiveRecord(parcelRecord);
  }
});

Copy standard line features into a parcel type

await QueuedTask.Run(async () =>
{
  // check for selected layer
  if (MapView.Active.GetSelectedLayers().Count == 0)
  {
    System.Windows.MessageBox.Show("Please select a target parcel polygon layer in the table of contents", "Copy Line Features To");
    return;
  }

  //first get the feature layer that's selected in the table of contents
  var destPolygonL = MapView.Active.GetSelectedLayers().First() as FeatureLayer; //a parcel polygon feature layer
  var fcDefinition = destPolygonL.GetFeatureClass().GetDefinition();
  GeometryType geomType = fcDefinition.GetShapeType();
  if (geomType != GeometryType.Polygon)
  {
    System.Windows.MessageBox.Show("Please select a target parcel polygon layer in the table of contents", "Copy Line Features To");
    return;
  }

  var layers = MapView.Active.Map.GetLayersAsFlattenedList();
  var srcFeatLyr = layers.FirstOrDefault(l => l.Name == "Lines" && l is FeatureLayer);
  var myParcelFabricLayer = layers.FirstOrDefault(l => l is ParcelLayer) as ParcelLayer;

  string destLineLyrName1 = destPolygonL.Name + "_Lines";
  string destLineLyrName2 = destPolygonL.Name + " Lines";
  string destLineLyrName3 = destPolygonL.Name + "Lines";

  var destLineL = layers.FirstOrDefault(l => l.Name == destLineLyrName1 && l is FeatureLayer);
  if (destLineL == null)
    destLineL = layers.FirstOrDefault(l => l.Name == destLineLyrName2 && l is FeatureLayer);

  if (destLineL == null)
    destLineL = layers.FirstOrDefault(l => l.Name == destLineLyrName3 && l is FeatureLayer);

  if (myParcelFabricLayer == null || destLineL == null || destPolygonL == null)
    return;

  var theActiveRecord = myParcelFabricLayer.GetActiveRecord();

  if (theActiveRecord == null)
  {
    System.Windows.MessageBox.Show("There is no Active Record. Please set the active record and try again.", "Copy Line Features To");
    return;
  }

  var editOper = new EditOperation()
  {
    Name = "Copy Line Features To Parcel Type",
    ProgressMessage = "Copy Line Features To Parcel Type...",
    ShowModalMessageAfterFailure = true,
    SelectNewFeatures = true,
    SelectModifiedFeatures = false
  };
  //add the standard feature line layers source, and their feature ids to a new KeyValuePair
  MapMember mapMemberSource = srcFeatLyr as MapMember;
  var ids = new List<long>((srcFeatLyr as FeatureLayer).GetSelection().GetObjectIDs());

  if (ids.Count == 0)
  {
    System.Windows.MessageBox.Show("No selected lines were found. Please select line features and try again.", "Copy Line Features To");
    return;
  }
  editOper.CopyLineFeaturesToParcelType(srcFeatLyr, ids, destLineL, destPolygonL);
});

Copy parcel lines to a parcel type

var theActiveRecord = myParcelFabricLayer.GetActiveRecord();

if (theActiveRecord == null)
{
  System.Windows.MessageBox.Show("There is no Active Record. Please set the active record and try again.", "Copy Line Features To");
  return;
}

var editOper = new EditOperation()
{
  Name = "Copy Lines To Parcel Type",
  ProgressMessage = "Copy Lines To Parcel Type ...",
  ShowModalMessageAfterFailure = true,
  SelectNewFeatures = true,
  SelectModifiedFeatures = false
};

MapMember mapMemberSource = srcParcelFeatLyr as MapMember;
var ids = new List<long>(srcParcelFeatLyr.GetSelection().GetObjectIDs());

if (ids.Count == 0)
{
  System.Windows.MessageBox.Show("No selected parcels found. Please select parcels and try again.", "Copy Parcel Lines To");
  return;
}
//add the standard feature line layers source, and their feature ids to a new KeyValuePair
var kvp = new KeyValuePair<MapMember, List<long>>(mapMemberSource, ids);
var sourceParcelFeatures = new List<KeyValuePair<MapMember, List<long>>> { kvp };
ParcelEditToken peToken = editOper.CopyParcelLinesToParcelType(myParcelFabricLayer, sourceParcelFeatures, destLineL, destPolygonL, null, true, false, true);
var createdIDsSelectionSet = peToken.CreatedFeatures;
await editOper.ExecuteAsync();

Assign features to active record

var guid = theActiveRecord.Guid;
var editOper= new EditOperation()
{
  Name = "Assign Features to Record",
  ProgressMessage = "Assign Features to Record...",
  ShowModalMessageAfterFailure = true,
  SelectNewFeatures = true,
  SelectModifiedFeatures = false
};
//add parcel type layers and their feature ids to a new KeyValuePair
MapMember mapMemberSource = srcFeatLyr as MapMember;
var ids = new List<long>(srcFeatLyr.GetSelection().GetObjectIDs());
var kvp = new KeyValuePair<MapMember, List<long>>(mapMemberSource, ids);
var sourceFeatures = new List<KeyValuePair<MapMember, List<long>>> { kvp };

editOper.AssignFeaturesToRecord(myParcelFabricLayer, sourceFeatures, guid, ParcelRecordAttribute.CreatedByRecord); 
await editOper.ExecuteAsync();

Create parcel seeds

var guid = theActiveRecord.Guid;
var editOper = new EditOperation()
{
  Name = "Create Parcel Seeds",
  ProgressMessage = "Create Parcel Seeds...",
  ShowModalMessageAfterFailure = true,
  SelectNewFeatures = true,
  SelectModifiedFeatures = false
};

editOper.CreateParcelSeeds(myParcelFabricLayer, MapView.Active.Extent, guid);
await editOper.ExecuteAsync();

Build parcels

var editOper = new EditOperation()
{
  Name = "Build Parcels",
  ProgressMessage = "Build Parcels...",
  ShowModalMessageAfterFailure = true,
  SelectNewFeatures = true,
  SelectModifiedFeatures = true
};
editOper.BuildParcelsByRecord(myParcelFabricLayer,guid);
await editOper.ExecuteAsync();

Duplicate parcels

//get the polygon layer from the parcel fabric layer type, in this case a layer called Tax
var targetFeatLyr = layers.FirstOrDefault(l => l.Name == "Tax" && l is FeatureLayer) as FeatureLayer;
MapMember mapMemberSource = sourcePolygonL as MapMember; //a parcel polygon feature layer
var ids = new List<long>(sourcePolygonL.GetSelection().GetObjectIDs());

if (ids.Count == 0)
{
  System.Windows.MessageBox.Show("No selected parcels found. Please select parcels and try again.", "Copy Parcel Lines To");
  return;
}

//add polygon layers and the feature ids to be duplicated to a new KeyValuePair
var kvp = new KeyValuePair<MapMember, List<long>>(mapMemberSource, ids);
var sourceFeatures = new List<KeyValuePair<MapMember, List<long>>> { kvp };
var theActiveRecord = myParcelFabricLayer.GetActiveRecord();

if (theActiveRecord == null)
{
  System.Windows.MessageBox.Show("There is no Active Record. Please set the active record and try again.", "Copy Line Features To");
  return;
}

Guid guid=theActiveRecord.Guid;
var editOper  = new EditOperation()
{
  Name = "Duplicate Parcels",
  ProgressMessage = "Duplicate Parcels...",
  ShowModalMessageAfterFailure = true,
  SelectNewFeatures = true,
  SelectModifiedFeatures = false
};
editOper.DuplicateParcels(myParcelFabricLayer, sourceFeatures, guid, targetFeatLyr, -1);
await editOper.ExecuteAsync();

Set parcels historic

MapMember mapMemberSource = destPolygonL as MapMember; //a parcel polygon feature layer
var ids = new List<long>(destPolygonL.GetSelection().GetObjectIDs()); 
//add polygon layers and the feature ids to be made historic to a new KeyValuePair
var kvp = new KeyValuePair<MapMember, List<long>>(mapMemberSource, ids);
var sourceFeatures = new List<KeyValuePair<MapMember, List<long>>> { kvp };

Guid guid = theActiveRecord.Guid;
var editOper = new EditOperation()
{
  Name = "Set Parcels Historic",
  ProgressMessage = "Set Parcels Historic...",
  ShowModalMessageAfterFailure = true,
  SelectNewFeatures = true,
  SelectModifiedFeatures = false
};

editOper.UpdateParcelHistory(myParcelFabricLayer, sourceFeatures, guid,true);
await editOper.ExecuteAsync();

Change parcel type

var targetFeatLyr = layers.FirstOrDefault(l => l.Name == "Tax" && l is FeatureLayer) as FeatureLayer; //the target parcel polygon feature layer

if (pfL == null || sourcePolygonL == null)
  return;

//add polygon layers and the feature ids to change the type on to a new KeyValuePair
MapMember mapMemberSource = sourcePolygonL as MapMember;
var ids = new List<long>(sourcePolygonL.GetSelection().GetObjectIDs());
var kvp = new KeyValuePair<MapMember, List<long>>(mapMemberSource, ids);
var sourceFeatures = new List<KeyValuePair<MapMember, List<long>>> { kvp };

var editOper = new EditOperation()
{
  Name = "Change Parcel Type",
  ProgressMessage = "Change Parcel Type...",
  ShowModalMessageAfterFailure = true,
  SelectNewFeatures = true,
  SelectModifiedFeatures = false
};

editOper.ChangeParcelType(pfL, sourceFeatures, targetFeatLyr, -1);
await editOper.ExecuteAsync();
Clone this wiki locally