Reusing Pro Command - question

463
5
Jump to solution
11-07-2023 04:11 AM
TorbjørnDalløkken2
Occasional Contributor

I need to use the alignMiddle-command in my ArcGIS Pro Add-in, for aligning some elements on the layout. I've seen the article about Reusing Pro Command in the ProGuide. When I add the button for "esri_layouts_alignMiddle" in config.daml I'm able to run the command. 

Is it possible to run this command without having to add the button to the ribbon?

 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

Yes, it is possible. Code below:

            var pluginWrapper = FrameworkApplication.GetPlugInWrapper(id, true) as System.Windows.Input.ICommand;
            if (pluginWrapper != null)
            {
                if (pluginWrapper.CanExecute(null))
                {
                    pluginWrapper.Execute(null);
                }
            }

More info here

View solution in original post

5 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Yes, it is possible. Code below:

            var pluginWrapper = FrameworkApplication.GetPlugInWrapper(id, true) as System.Windows.Input.ICommand;
            if (pluginWrapper != null)
            {
                if (pluginWrapper.CanExecute(null))
                {
                    pluginWrapper.Execute(null);
                }
            }

More info here

Wolf
by Esri Regular Contributor
Esri Regular Contributor

There is a community sample that shows different variations of reusing ProCommands:

 

arcgis-pro-sdk-community-samples/Framework/ReusingProCommands at master · ArcGIS/arcgis-pro-sdk-comm...

 

0 Kudos
TorbjørnDalløkken2
Occasional Contributor

@Wolf , @GKmieliauskas Does anyone of you know which conditions that needs to be met for the command "esri_layouts_alignMiddle" to be executed?

I've selected to elements from my layout, but CanExecute returns false

var tmpTextElement = Layout.FindElement(element.Name);
var tmpPunktElement = Layout.FindElement(PunktElement.Name);
Layout.SelectElements(new List<Element> { tmpTextElement, tmpPunktElement });

var pluginWrapperDistributeAlignMiddle = FrameworkApplication.GetPlugInWrapper("esri_layouts_alignMiddle", true) as ICommand;
if (pluginWrapperDistributeAlignMiddle != null)
{
   if (pluginWrapperDistributeAlignMiddle.CanExecute(null))
   {
      pluginWrapperDistributeAlignMiddle.Execute(null);
   }
}

  

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

As far as i can tell from looking at ArcGISLayout.daml the following condition has to be met:

id="esri_layouts_selectedElementsNotPartsCondition"

The description for the condition says:

"Elements are selected in the active layout view and FORMAT selection is set to the whole element not a part."

I used your code and it's working fine for me:

protected override void OnClick()
{
  try
  {
    LayoutView layoutView = LayoutView.Active;
    Layout layout = layoutView.Layout;

    var tmpTextElement = layout.FindElement("TestText 1");
    var tmpPunktElement = layout.FindElement("Point");
    layout.SelectElements(new List<ArcGIS.Desktop.Layouts.Element> { tmpTextElement, tmpPunktElement });
    MessageBox.Show($@"Selected: {layout.GetSelectedElements().Count}");

    var pluginWrapperDistributeAlignMiddle = FrameworkApplication.GetPlugInWrapper("esri_layouts_alignMiddle", true) as ICommand;
    if (pluginWrapperDistributeAlignMiddle != null)
    {
      if (pluginWrapperDistributeAlignMiddle.CanExecute(null))
      {
        pluginWrapperDistributeAlignMiddle.Execute(null);

      }
      else
        MessageBox.Show("Can't execute");
    }
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}

These are my two sample elements to be aligned:

Wolf_0-1704987494759.png

After the alignment button completes i see this:

Wolf_1-1704987554007.png

 

 

0 Kudos
TorbjørnDalløkken2
Occasional Contributor

Thank you for testing, @Wolf  and @GKmieliauskas .

I've now tried adding your code, @Wolf , and I get a confirmation that 2 elements in the layout is selected. However, the CanExecute(null) returns false and the elements are not aligned. You mentioned the condition-description: 

"Elements are selected in the active layout view and FORMAT selection is set to the whole element not a part."

Since there are elements selected in the active layout view, then it must be something to do with the FORMAT selection. How do I set this to the whole element and not a part?

Do you have any other suggestions on how to align these, without the use of the align-command?

 

0 Kudos