Hello community
Im new developing in inventor, Im building a part add-in where open a form in modeless, until here everything is good, then I need to get the face that user selects in object, and change some values in the form depend of selected face. I need every time that the user select a face get values and update textbox in the form. someone have any idea how I can achive this? I am working with c#. I have this classs but I dont know in what place create the object.
public class Select
{
private InteractionEvents interactionEvents;
private SelectEvents selectEvents;
private MouseEvents oMouseEvents;
bool bStillSelecting;
public Object Pick(SelectionFilterEnum filter, Application app)
{
dynamic _return = null;
bStillSelecting = true;
//Create an InteractionEvents object.
interactionEvents = app.CommandManager.CreateInteractionEvents();
interactionEvents.OnTerminate += InteractionOnTerminate;
//Define that we want select events rather than mouse events.
interactionEvents.SelectionActive = true;
interactionEvents.InteractionDisabled = false;
//Set a reference to the select events.
selectEvents = interactionEvents.SelectEvents;
//oMouseEvents = interactionEvents.MouseEvents;
//oMouseEvents.MouseMoveEnabled = true;
//Set the filter using the value passed in.
selectEvents.AddSelectionFilter(filter);
selectEvents.OnSelect += SelectionOnSelect;
//The InteractionEvents object.
interactionEvents.Start();
//Loop until a selection is made.
do
{
app.UserInterfaceManager.DoEvents();
} while (bStillSelecting);
//Get the selected item. If more than one thing was selected,
//just get the first item and ignore the rest.
ObjectsEnumerator oSelectedEnts = selectEvents.SelectedEntities;
_return = oSelectedEnts.Count > 0 ? oSelectedEnts[1] : null;
//Stop the InteractionEvents object.
interactionEvents.Stop();
selectEvents = null;
interactionEvents = null;
return _return;
}
private void SelectionOnSelect(Inventor.ObjectsEnumerator JustSelectedEntities, Inventor.SelectionDeviceEnum SelectionDevice, Inventor.Point ModelPosition, Inventor.Point2d ViewPosition, Inventor.View View)
{
bStillSelecting = false;
}
private void InteractionOnTerminate()
{
bStillSelecting = false;
}
}
Thanks so much for your help
Solved! Go to Solution.
Solved by Michael.Navara. Go to Solution.
It is much better to keep selection event-based instead of use DoEvents and try to block method execution. Below is the sample implementation of dialog with selection sample. Designer part of dialog code is missing. I expect you create your own form like this and assign your own button click event handler. You need to assign dialog property InventorApp from the place where you create the dialog.
Dialog preview
Create, set and show the dialog
void Main()
{
var dlg = new IC_12689846Dlg();
dlg.InventorApp = ThisApplication;
dlg.Show();
}
Dialog and Selector class code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Inventor;
using Application = Inventor.Application;
using Point = Inventor.Point;
using View = Inventor.View;
namespace iLogicForCSharp.InventorCustomization
{
public partial class IC_12689846Dlg : Form
{
private Selector selector;
public IC_12689846Dlg()
{
//Form with
// one button: button1
// one text box: textBox1
InitializeComponent();
}
public Application InventorApp { get; set; }
private void button1_Click(object sender, EventArgs e)
{
selector = new Selector(InventorApp);
selector.OnSelectionFinished += Selector_OnSelectionFinished;
selector.StartSelection(SelectionFilterEnum.kPartFacePlanarFilter, false);
}
private void Selector_OnSelectionFinished(object sender, OnSelectionFinishedHandlerArgs args)
{
textBox1.Text = args.Faces.Sum(x => x.Evaluator.Area).ToString();
}
}
internal class Selector
{
private readonly Application inventor;
private List<Face> faces = new List<Face>();
private InteractionEvents interactionEvents;
private SelectEvents selectEvents;
private bool singleSelect;
public Selector(Application inventor)
{
this.inventor = inventor;
}
public event OnSelectionFinishedHandler OnSelectionFinished;
public void StartSelection(SelectionFilterEnum filter, bool singleSelect)
{
this.singleSelect = singleSelect;
//Create an InteractionEvents object.
interactionEvents = inventor.CommandManager.CreateInteractionEvents();
interactionEvents.OnTerminate += InteractionOnTerminate;
//Define that we want select events rather than mouse events.
interactionEvents.SelectionActive = true;
interactionEvents.InteractionDisabled = false;
//Set a reference to the select events.
selectEvents = interactionEvents.SelectEvents;
//oMouseEvents = interactionEvents.MouseEvents;
//oMouseEvents.MouseMoveEnabled = true;
//Set the filter using the value passed in.
selectEvents.AddSelectionFilter(filter);
selectEvents.OnSelect += SelectionOnSelect;
//The InteractionEvents object.
interactionEvents.Start();
}
private void FireResultEvent()
{
interactionEvents.OnTerminate -= InteractionOnTerminate;
selectEvents.OnSelect -= SelectionOnSelect;
interactionEvents.Stop();
interactionEvents = null;
selectEvents = null;
OnSelectionFinished?.Invoke(this, new OnSelectionFinishedHandlerArgs(faces));
}
private void InteractionOnTerminate()
{
FireResultEvent();
}
private void SelectionOnSelect(ObjectsEnumerator justSelectedEntities, SelectionDeviceEnum selectionDevice,
Point modelPosition, Point2d viewPosition, View view)
{
faces.AddRange(justSelectedEntities.OfType<Face>());
if (singleSelect)
FireResultEvent();
}
}
internal delegate void OnSelectionFinishedHandler(object sender, OnSelectionFinishedHandlerArgs args);
internal class OnSelectionFinishedHandlerArgs
{
private readonly List<Face> faces;
public OnSelectionFinishedHandlerArgs(List<Face> faces)
{
this.faces = faces;
}
public Face[] Faces => faces.ToArray();
}
}
Hi Michael, thank you for your help, Im going to do a implementation with the code you send me, I was analyzed it and this have sense.
Best regards
Can't find what you're looking for? Ask the community or share your knowledge.