Esempio n. 1
0
        // Search button is clicked: Traverse the DAG and put results into the Grid
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            // Script in which to embed the lambda written by the user
            string MyScript = @"using System;
                                using System.Collections.Generic;
                                using System.Linq;
                                using System.Text;

                                using Autodesk.Maya.Runtime;
                                using Autodesk.Maya.OpenMaya;
                                using Autodesk.Maya;
            
                                public class Script
                                {
                                    delegate bool QueryFunc(MDagPath dp);

                                    public System.Collections.Generic.IEnumerable<MDagPath> Main()
                                    {
                                        var dag = new MCDag();

                                        QueryFunc myLambda = (dagpath) => " + textBox1.Text.Trim() + @";

                                        var elements = from dagpath in dag.DagPaths where myLambda(dagpath) select dagpath;

                                        return elements;
                                    }
                                }";
            // Run it
            Object ObjList = Run("C#", MyScript);

            // If there was no error
            if (ObjList != null)
            {
                using (new CursorSwitcher(null)) {
                    try {
                        ResultGrid.Items.Clear();
                    } catch {
                    }

                    // Get whatever's returned by the script
                    var ObjEnum = ObjList as IEnumerable <MDagPath>;
                    // Were some objects returned?
                    if (!ObjEnum.Any <MDagPath> ())
                    {
                        MessageBox.Show("No object returned.", "DAG Explorer", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        LinkedList <MayaObject> myList;
                        HashSet <MayaObjPropId> myProps;
                        GatherObjects(ObjEnum, out myList, out myProps);
                        int i;

                        // Setup the grid data columns if it isn't done already
                        if (ResultGrid.Columns.Count < 2)
                        {
                            i = 0;
                            DataGridTextColumn col;
                            foreach (var p in myProps)
                            {
                                col = new DataGridTextColumn();
                                ResultGrid.Columns.Add(col);
                                col.Header  = p.name;
                                col.Binding = new Binding("[" + i + "]");

                                i++;
                            }
                        }
                        // Add all the rows, one per object
                        foreach (var Obj in myList)
                        {
                            Object [] arr = new Object [myProps.Count];

                            i = 0;
                            foreach (var p in myProps)
                            {
                                MayaObjPropVal mopv;
                                // Search for the property in the object
                                if (Obj.properties.TryGetValue(p.name, out mopv))
                                {
                                    arr [i] = mopv.value;
                                }
                                else
                                {
                                    arr [i] = "";
                                }

                                i++;
                            }
                            ResultGrid.Items.Add(arr);
                        }
                        TabControl1.BringIntoView();
                        tabItem1.IsSelected = true;
                    }
                }
            }
        }