Microsoft 开发人员网络 > 论坛主页 > Visual Studio Extensibility > How do I select a DSL shape via API
提出问题提出问题
 

已答复How do I select a DSL shape via API

  • 2009年7月1日 13:53AtulKatare 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi All

    I want select particular Shape programmatically...

    public void SelectShape(ModelElement modelElement)

        {

            ShapeElement modelElementShape = PresentationViewsSubject .GetPresentation(modelElement).FirstOrDefault() as ShapeElement ;

     

            Diagram diagram = modelElement.Store.ElementDirectory.AllElements.OfType<Diagram >().FirstOrDefault();

            DiagramItem diagramItem = new DiagramItem (modelElementShape);

            diagram.ActiveDiagramView.Selection.Set(diagramItem);

        }

     

    but diagram.ActiveDiagramView value is null ..

    what is wrong with my code?

     

     

    Thank You

     

    Atul Katare

    _____________________________

    Build Relationship Dynamically

     

答案

  • 2009年7月2日 15:33RubenJMarrufo 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Hello, DslTools generate a CommandSet class in your DslPackage that you can extend with your own commands. I suppose you want to select the shape responding to some command action. So there, in YourLanguageCommandSet, you can get Store by this.CurrentDocData.Store. Regards!!
    RubenBrenes

全部回复

  • 2009年7月1日 22:26Sebastian Dyck 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi Atul,

    could you please provide some additional information, especially with respect to the context you are invoking this method from and your general porpuse? I tried to reproduce the issue and diagram.ActiveDiagramView returned the correct object.

    Kind regards
    Sebastian
  • 2009年7月2日 5:10AtulKatare 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi Sebastian

    I will get Model Element using

                Store store = new Store(typeof(CoreDomainModel), typeof(CoreDesignSurfaceDomainModel),
                                                                                                 typeof(ExampleDomainModel));
                using (Transaction txtTransaction = store.TransactionManager.BeginTransaction("Root Transaction"))
                {
                    ExampleModel ExampleRoot = ExampleSerializationHelper.Instance.LoadModelAndDiagram
                                                    (store, FilePath, FilePath + ".diagram", null, null);

                   
                   ModelElement modelElement= ExampleRoot .ExampleElement.First();

                 //  then I will call SelectShape Method

                 Selectshape(modelElement);

                    txtTransaction .Commit();
                }

    public void SelectShape(ModelElement modelElement)

        {

            ShapeElement modelElementShape = PresentationViewsSubject .GetPresentation(modelElement).FirstOrDefault() as ShapeElement ;

     

            Diagram diagram = modelElement.Store.ElementDirectory.AllElements.OfType<Diagram >().FirstOrDefault();

            DiagramItem diagramItem = new DiagramItem (modelElementShape);

            diagram.ActiveDiagramView.Selection.Set(diagramItem);

        }



    Thank You

    AtulKatare
    _________________________________
    Build Relationship Dynamically
  • 2009年7月2日 12:19RubenJMarrufo 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     建议的答复
    Hello, you are opening a model into a new Store, so this Store doesn't have the current active diagram view. I think if you take from the commandset the current opened store and then you get the active diagram view from the diagram, this object wouldn't be null. Regards!!


    RubenBrenes
  • 2009年7月2日 14:11AtulKatare 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi Ruben

    How to get Store from CommandSet class?
    CommandSet class constructor require IServiceProvider, how to pass value?

    Thank You..
  • 2009年7月2日 15:33RubenJMarrufo 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Hello, DslTools generate a CommandSet class in your DslPackage that you can extend with your own commands. I suppose you want to select the shape responding to some command action. So there, in YourLanguageCommandSet, you can get Store by this.CurrentDocData.Store. Regards!!
    RubenBrenes
  • 2009年7月8日 13:41Kiran Chand Palakkattiri 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     建议的答复包含代码
    Dude, you should not create a new store for this requirement.

    Instead, here you go!

    1. Go to your DSL Package
    2. Create a partial class for your CommandSet
    3. Write a new method, let the name to be what ever you want
    4. Write your logic based on this.

    I'll give you a sample piece of code for select all command which will work directly for you.


    namespace YourNameSpace
    {
        /// <summary>
        /// Partial implementation provides the status and command
        /// handlers for Select All Menu
        /// </summary>
        internal partial class YourCommandSet
        {
            /// <summary>
            /// Handler for select all
            /// </summary>
            /// <param name="sender">the object that invokes the method as object</param>
            /// <param name="e">event arguments as EventArgs</param>
            protected override void ProcessOnMenuSelectAllCommand()
            {
    
                base.ProcessOnMenuSelectAllCommand();
    
                List<ShapeElement> lstSelectableElements = this.GetSelectableElements()
                    ?? new List<ShapeElement>();
    
                foreach (ShapeElement oShape in lstSelectableElements)
                {
                    if (oShape.ModelElement != null)
                    {
                        DiagramItem diagramItem = new DiagramItem(oShape);
                        if (this.CurrentYourModelDocView!= null
                            && this.CurrentYourModelDocView.Diagram != null
                            && this.CurrentYourModelDocView.Diagram.ActiveDiagramView != null
                            && this.CurrentYourModelDocView.Diagram.ActiveDiagramView.DiagramClientView != null)
                        {                        
                            this.CurrentYourModelDocView.Diagram.ActiveDiagramView.DiagramClientView.Selection.Add(diagramItem);
                        }
                    }
                }
    
    
                this.CurrentDocView.SetSelectedComponents(lstSelectableElements);
    
            }
            /// <summary>
            /// Status handler for select all
            /// </summary>
            /// <param name="command">command invokes the method</param>
            protected override void ProcessOnStatusSelectAllCommand(MenuCommand command)
            {
    
                base.ProcessOnStatusSelectAllCommand(command);
    
                List<ShapeElement> lstSelectableElements = this.GetSelectableElements()
                    ?? new List<ShapeElement>();
    
                command.Enabled = lstSelectableElements.Count > 0;
            }
    
            /// <summary>
            /// Gets the list of the selectable elements
            /// </summary>
            /// <returns>gets the list of the selectable shape elements as generic collection of shape element</returns>
            private List<ShapeElement> GetSelectableElements()
            {
    
                ReadOnlyCollection<ShapeElement> lstShapeElements = null;
                List<ShapeElement> lstSelectableElements = null;
                Store oStore = null;
    
                oStore = this.CurrentYourModelDocView.Diagram.Store;
    
                lstShapeElements = oStore.ElementDirectory.FindElements<ShapeElement>(true);
                lstShapeElements = lstShapeElements ?? new ReadOnlyCollection<ShapeElement>(new List<ShapeElement>());
    
                lstSelectableElements = (from shape in lstShapeElements
                                         where shape is NodeShape
                                         select shape).ToList();
    
                return lstSelectableElements;
            }
        }
    }
    
    Try this; You have everything selected on Select All (Ctl + A)
    --Kiran Chand Palakkattiri--
    kiran.chand@live.in