walkinside / sdk

Develop powerful desktop and web applications on top of Walkinside 3D engine
4 stars 4 forks source link

Context menu in CAD hierarchy branch #41

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hello All,

To create context menu in 3D object by below, interface: IVRViewerSdk ToolStripItem a = viewer.UI.Control.ContextMenuStrip.Items.Add("Menu Name");

But how to create context menu in CAD Hierarchy? Enclosed for reference to where i need to create it. menu_in_cad_hierarchy

Thanks, Dinesh

kveretennicov commented 6 years ago

Hello @dineshkj,

Current Viewer SDK does not provide any extension points for plug-ins to add menu items to this context menu. We will consider supporting this use case in future releases. What kind of command do you have in mind?

CC @walkinside/devs

ghost commented 6 years ago

Hello Kveretennicov, Thank you, I look for some menu like Navigating to COMOS from CAD hierarchy context menu. This is available in 3D object. navigate to comos

Viewer SDK is not having an option. Is there anything else can be used like branch manager etc. If we find the form and branches of CAD hierarchy. What i expect can be implemented.

Thanks, Dinesh

ghost commented 6 years ago

Hi, Does anyone has an idea??? Please share.

Thanks, Dinesh

kveretennicov commented 6 years ago

@dineshkj,

As explained above, there is no way to achieve this with the current API. We will give a serious consideration to your use case and may add a new API to support it in future releases of Walkinside Viewer SDK.

You may be tempted to try reflection or other hacks to access the context menu. It's not a solution we can endorse though, because there is no guarantee that it will work or that it will continue to work in new versions of Walkinside.

griemens commented 6 years ago

Hi Dineshkj,

As kveretennicov mentioned using reflection there is no guarantee at all it will be compatible with next versions of Walkinside. So if you realy realy need access to the menu.

Here is something you could try. This worked for me in 10.2.34 using VS2017 watch debugger. (But again no guarantees on older versions or future versions) Basic Concept:

Here we go Use SDKViewer.UI.DockPanel.ContentAdded += DockPanel_ContentAdded; to get notified when a new VRForm is created. Important: Do not forget to cleanup by implementing for each += an -= so Walkinside will not crash when your plugin is unloaded.

Check if the form is CAD or FRT by

private void DockPanel_ContentAdded(object sender, WeifenLuo.WinFormsUI.Docking.DockContentEventArgs e)
{
  var form = e.Content.DockHandler.Form;
  if (form is VRForm)
  {
    if (form.GetType().ToString() == "vrcontext.walkinside.VRBranchForm")
    {
      form.Shown += Form_Shown;
    }
  }
}

Now to get access to the actual ContextMenuStrip you need to drill down deep in the controls.

private void Form_Shown(object sender, EventArgs e)
{
            VRForm f = sender as VRForm;
            var controls = f.Controls;
            foreach (var control in controls)
            {
                if (control is SplitContainer)
                {
                    SplitContainer container = control as SplitContainer;
                    foreach (var panel in container.Panel1.Controls)
                    {
                        if (panel is Panel)
                        {
                            foreach (var treecontrol in ((Panel)panel).Controls)
                            {
                                TreeView treeview =  treecontrol as TreeView;
                                if (treeview != null)
                                {
                                    if (treeview.ContextMenuStrip != null)
                                    {
                                        treeview.ContextMenuStrip.Items.Add("My Menu");
                                    }
                                }
                            }
                        }
                    }
                }
            }
}
ghost commented 6 years ago

Hello kveretennicov,

Its good to hear from you, Thanks for support..

Thanks, Dinesh

ghost commented 6 years ago

Hello griemens,

I could understand the issue, Thanks a lot for the code & support.

Thanks, Dinesh

ghost commented 6 years ago

Hello,

Code is working good, Thanks... How to get the branch name by click on added menu in CAD Hierarchy?

    ToolStripItem x = null;

    private void Form_Shown(object sender, EventArgs e)
    {
        //Above code

        //Add plugin
        if (treeview.ContextMenuStrip != null)
        {
            x = treeview.ContextMenuStrip.Items.Add("My menu");
            x.Click += this.Menu_Click;
        }
        //Remove plugin
        else if (treeview.ContextMenuStrip == null)
        {
            m_Item.Click -= this.Menu_Click;
            treeview.ContextMenuStrip.Items.Remove(m_Item);
        }
    }

    //Event
    private void Menu_Click(object sender, EventArgs e)
    {
        //--------How to get the value of branch name from CAD Hierarchy----------
        //string tagname = ???;
    }

Thanks, Dinesh

griemens commented 6 years ago

Hi Dinesh,

Not 100% sure but I believe the Branch is always selected. So if you have OnBranchSelect eventhandler it should receive the branch where contextmenu is acted upon. Alternative I could see in VS debugger that the TreeView.SelectedNode.Tag is of type IVRBranch.

Note: When going the reflection route the VS debugger watch values is your best friend :)

Cheers

ghost commented 6 years ago

Hi Griemens,

Thanks for support, I used to below code to get the name of selected node. FYI

ToolStripItem m_Item = null;
private void Form_Shown(object sender, EventArgs e)
{
    //Above code

    //Add plugin
    if (treeview.ContextMenuStrip != null)
    {
        x = treeview.ContextMenuStrip.Items.Add("My menu");
        x.Click += (sender1, e1) => Menu_Click(sender1, e1, treeview);
    }
}

//Event
private void Menu_Click(object sender, EventArgs e, TreeView treeview1)
{
    string tagname = treeview1.SelectedNode.Text;
}

Cheers, Dinesh