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?
Solved! Go to Solution.
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
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
There is a community sample that shows different variations of reusing ProCommands:
@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);
}
}
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:
After the alignment button completes i see this:
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?