3

Spatial Adjustment>Attribute Transfer Tool in ArcMap transfers attributes between mapped source and target layers by mouse clicks. The disadvantage is you can do one point/line at a time. My problem is to transfer attributes from a single line (a street) to multiple selected points (selected address points).

I was looking into the documentation of IAttribute Transfer: https://desktop.arcgis.com/en/arcobjects/latest/net/webframe.htm#IAttributeTransfer.htm

I would like to click on the street line and then select multiple address points so that attributes from street will be automatically transferred to selected points- same way as the tool currently works for one point.

 IMxDocument mxDoc = m_app.Document as IMxDocument;
  IMap map = mxDoc.FocusMap;
   if (m_app == null)
    return;
  IEditor editor = m_app.FindExtensionByName("ESRI Object Editor") as IEditor;

  //Cast for Attribute Transfer Type and get IAttributeTransfer.
  IAttributeTransferType attTransType = editor as IAttributeTransferType;
  IAttributeTransfer attTransfer = attTransType.AttributeTransfer;
  //Cast for default settings and return source/target layers.
  IAttributeTransferDefaultSettings attTransDefSettings = attTransfer as               
  IAttributeTransferDefaultSettings;
  IFeatureLayer2 sourceFL = ReturnLayer(map, attTransDefSettings.SourceName);
  //If no mapping set, sourceFL will be null (null sourcename) so bail
  if (sourceFL == null)
  {
    System.Windows.Forms.MessageBox.Show
      ("Attribute Transfer field mapping not set");
    return;
  }
  //Get target feature layer.
  IFeatureLayer2 tFLayer = ReturnLayer(map, attTransDefSettings.TargetName);
  //Get the fieldmap for the source layer.
  IFieldMap fieldMap = attTransfer.FindFieldMap(sourceFL.FeatureClass, null);
  //Enumerate through each selected feature (selection from source layer)
  IEnumFeature featEnum = editor.EditSelection as IEnumFeature;
  featEnum.Reset();
  IFeature sourceFeature = featEnum.Next();
  if (sourceFeature == null)
  {
    System.Windows.Forms.MessageBox.Show("Please Selecet a Source Feature");
  }
  editor.StartOperation();
  while (sourceFeature != null)

  {
//TODO: Click on source line and then select multiple target points, which transfers attributes

    try
    {
      if (targetFeature != null)
      {           
        attTransfer.Transfer(fieldMap, sourceFeature, targetFeature, out success);
      }
    }
    catch (System.Exception ex)
    {
      MessageBox.Show(ex.Message);
    }
    sourceFeature = featEnum.Next();
  }
  editor.StopOperation("Attribute Transfer");
}

However, that code is looking into automated transfer of overlaying lines. I wanted to interactively select source line and target points by clicking and transferring just as Attribute Transfer tool does (in addition to do multiple selection of points) Any pointers or sample codes to achieve this? I am really stuck.

  • 1
    I've done such but the code is far more than just a snippet, you'd have to get way more specific with your own code before I can offer any help here and nominate which of the ArcObjects platforms you're working with. You could do this quickly in arcpy with creating the selection (line and points) interactively then use a searchcursor on the lines and an updatecursor on the points - paste the code into your ArcMap python window then hit enter; any time you want to do it again put your cursor in the previous block and hit enter twice to save pasting all the time. – Michael Stimson Feb 26 at 4:45
  • With my limited knowledge, I tried to elaborate the post with addition of the code with a pointer to where I am stuck. – Ann Feb 26 at 14:33
  • 1
    Like Michael said this question's scope is too large, but I can give you some rought steps. It looks like you want to build an ArcMap Add-In. Since you're using C# I assume you're using Visual Studio. If the Arc SDK is installed properly you should be able to create an Desktop Add-In project. In the most simple and rigid case, you'd want to create a button that looks at the selected features on the map and then transfers attributes from a selected line to all selected points. – danielm Feb 26 at 16:24
  • That makes sense- I feel like I was over complicating the thought process with a tool which replicates Attribute Transfer for multiple selections. Let me think in that direction and see how I can update Attributes for selection using the button – Ann Feb 26 at 17:11
  • The start is to create a tool resources.arcgis.com/en/help/arcobjects-net/componenthelp/… from the Esri addin template installed with the SDK, you can respond to events: protected override void OnMouseDown(MouseEventArgs arg), arg.x and arg.y are in screen units, use an IDisplayTransformation to get map units. When I did this one I had right-click to grab, left click to apply but I had to override the context menu. It sounds like you're jumping in the deep end and I respect that, good luck but make your q's more specific in the future. – Michael Stimson Feb 26 at 23:42
2

To correctly implement ArcObjects you must use the right template installed with the Esri SDK: enter image description here

--and then--

enter image description here

Buttons and tools look fairly similar but there are significant differences:

  • Tools activate, buttons click.
  • Tools stay active until the next tool is activated, a button does something then exits.
  • While the tool is active it will be able to listen for events.

As requested, the stripped down left/right click:

protected override void OnMouseDown(MouseEventArgs arg)
    {
        if (gEd.EditState != esriEditState.esriStateEditing) return;
        if (arg.Button == System.Windows.Forms.MouseButtons.Left)
        {
            gMouseIsDown = true;
            gMouseDownPnt.SetCoords(arg.X, arg.Y);
            gTracking = false;
        }
        else if (arg.Button == System.Windows.Forms.MouseButtons.Right)
        {
            // get the point in screen coordinates
            IPoint pQueryPoint = new PointClass();
            pQueryPoint = gDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);

        }
    }

There is also an OnMouseUp and OnMouseMove event but try to use the mouse move sparingly as this event fires a lot and if you're performing an action every time the mouse moves your cursor will seem to be swimming in mud.

Important: if you are listening for the right button click then you have to tell ArcMap not to implement its own context menu:

protected override bool OnContextMenu(int x, int y)
{
    return true; // dismiss any context menu
}
| improve this answer | |
  • Perfect- Thank you! – Ann Feb 28 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.