[C#.NET AddIn CONNECT] How to assign a GraphicGroup to a new linestring

Hi there,

I want to create a linestring and add it to the model in a certain graphicgroup.
Would anyone know how to do this? Problem is that ElementPropertiesSetter class does not seem to have something for this.
In the function to create the linestring, I have this:

    ElementPropertiesSetter propsSetter = new ElementPropertiesSetter();
    propsSetter.SetLevel(Bentley.MstnPlatformNET.Settings.ActiveLevelId);
    //  ...
    //Non existent:
    //propsSetter.SetGraphicGroup(Bentley.MstnPlatformNET.Settings.CurrentGraphicGroup);
    
    var retElem = new LineStringElement(Session.Instance.GetActiveDgnModel(), null, m_points.ToArray());
    propsSetter.Apply(retElem);

Best regards, Jan Willem

Parents
  • P.S. I have found a crazy way, using the COM interface, in the part where the element is added to the model.
    But it will make Jan Slegr shiver, I'm afraid; I am curious to know if there is something less ugly.

    Best regards, Jan Willem

    public static Bentley.Interop.MicroStationDGN.Application s_App;
    s_App = (Bentley.Interop.MicroStationDGN.Application)System.Runtime.InteropServices.Marshal.GetActiveObject(
    					"MicroStationDGN.Application");
    
    LineStringElement element = PrivCreateLinestr();
    if (element != null)
    {
    	element.AddToModel();
    
    	// Now the element has an id
    	var interopElem = s_App.ActiveModelReference.GetElementByID(element.ElementId);
    	interopElem.GraphicGroup = Bentley.MstnPlatformNET.Settings.CurrentGraphicGroup;
    	interopElem.Rewrite();
    
    	//note that interopElem is a Bentley.Interop.MicroStationDGN.Element,
    	//which is different from a Bentley.DgnPlatformNET.Elements.Element
    }
    

  • Have you considered using NamedGroups instead of graphic groups?  My guess is that bentley is trying to do away with basic graphic groups.

    Here is a handy extension method for the Element class

    /// <summary>
            /// Appends a plotted elements to the named group specified
            /// </summary>
            /// <param name="elements">The elements to append to the named group</param>
            /// <param name="groupName">The name of the name group to append to</param>
            /// <param name="description">The named group description. *Used only for named group creation*</param>
            /// <returns></returns>
            public static NamedGroup AddToNamedGroup(this IEnumerable<Element> elements, string groupName, string description)
            {
                DgnModel activeModel = Ms.GetActiveModel();
                NamedGroupCollection namedGroupCollection = new NamedGroupCollection(activeModel);
                NamedGroup namedGroup = namedGroupCollection.FindByName(groupName);
    
                if (namedGroup == null)
                {
                    NamedGroupFlags groupFlags = new NamedGroupFlags();
                    namedGroup = new NamedGroup(groupName, description, groupFlags, activeModel);
                }
    
                NamedGroupMemberFlags memberFlags = new NamedGroupMemberFlags();
    
                foreach (Element element in elements)
                    namedGroup.AddMember(element.ElementId, activeModel, memberFlags);
    
                namedGroup.WriteToFile(true);
                return namedGroup;
    
            }
    
            /// <summary>
            /// Appends a plotted element to the named group specified
            /// </summary>
            /// <param name="ele">The element to append to the named group</param>
            /// <param name="groupName">The name of the name group to append to</param>
            /// <param name="description">The named group description. *Used only for named group creation*</param>
            /// <returns></returns>
            public static NamedGroup AddToNamedGroup(this Element ele, string groupName, string description)
            {
                DgnModel activeModel = Ms.GetActiveModel();
                NamedGroupCollection namedGroupCollection = new NamedGroupCollection(activeModel);
                NamedGroup namedGroup = namedGroupCollection.FindByName(groupName);
    
                if (namedGroup == null)
                {
                    NamedGroupFlags groupFlags = new NamedGroupFlags();
                    namedGroup = new NamedGroup(groupName, description, groupFlags, activeModel);
                }
    
                NamedGroupMemberFlags memberFlags = new NamedGroupMemberFlags();
                namedGroup.AddMember(ele.ElementId, activeModel, memberFlags);
                namedGroup.WriteToFile(true);
                return namedGroup;
    
            }

Reply
  • Have you considered using NamedGroups instead of graphic groups?  My guess is that bentley is trying to do away with basic graphic groups.

    Here is a handy extension method for the Element class

    /// <summary>
            /// Appends a plotted elements to the named group specified
            /// </summary>
            /// <param name="elements">The elements to append to the named group</param>
            /// <param name="groupName">The name of the name group to append to</param>
            /// <param name="description">The named group description. *Used only for named group creation*</param>
            /// <returns></returns>
            public static NamedGroup AddToNamedGroup(this IEnumerable<Element> elements, string groupName, string description)
            {
                DgnModel activeModel = Ms.GetActiveModel();
                NamedGroupCollection namedGroupCollection = new NamedGroupCollection(activeModel);
                NamedGroup namedGroup = namedGroupCollection.FindByName(groupName);
    
                if (namedGroup == null)
                {
                    NamedGroupFlags groupFlags = new NamedGroupFlags();
                    namedGroup = new NamedGroup(groupName, description, groupFlags, activeModel);
                }
    
                NamedGroupMemberFlags memberFlags = new NamedGroupMemberFlags();
    
                foreach (Element element in elements)
                    namedGroup.AddMember(element.ElementId, activeModel, memberFlags);
    
                namedGroup.WriteToFile(true);
                return namedGroup;
    
            }
    
            /// <summary>
            /// Appends a plotted element to the named group specified
            /// </summary>
            /// <param name="ele">The element to append to the named group</param>
            /// <param name="groupName">The name of the name group to append to</param>
            /// <param name="description">The named group description. *Used only for named group creation*</param>
            /// <returns></returns>
            public static NamedGroup AddToNamedGroup(this Element ele, string groupName, string description)
            {
                DgnModel activeModel = Ms.GetActiveModel();
                NamedGroupCollection namedGroupCollection = new NamedGroupCollection(activeModel);
                NamedGroup namedGroup = namedGroupCollection.FindByName(groupName);
    
                if (namedGroup == null)
                {
                    NamedGroupFlags groupFlags = new NamedGroupFlags();
                    namedGroup = new NamedGroup(groupName, description, groupFlags, activeModel);
                }
    
                NamedGroupMemberFlags memberFlags = new NamedGroupMemberFlags();
                namedGroup.AddMember(ele.ElementId, activeModel, memberFlags);
                namedGroup.WriteToFile(true);
                return namedGroup;
    
            }

Children
  • Hi Maury,

    Thanks for youur suggestion about NamedGroups, and your code but I really hope Bentley will not abandon GG in it's API's. Which to me seems not to be their intention, as the ElementPropertiesGetter class deals with it, as Jan points out. Just the ElementPropertiesSetter doesn't (it was forgooten?)

    From mdl, I know NamedGroups and frankly I will try to avoid those. Certainly if it's just about simple grouping of elements together.The pain is the distributed way of maintaing the information, having the extra namedgroup element involved, and the project concerned was using other complicated stuff (interactions directly adjusting element descriptors while writing to file), and together it was quite some labyrintal mess. While GG is just an element property.

  • My guess is that bentley is trying to do away with basic graphic groups.

    That's unlikely.  The GG value is baked into the ubiquitous MSElement struct.  It's likely to remain indefinitely to support legacy usage and apps.

    But, as you suggest, Named Groups (NG) provide a 21st century enhancement.  The GG is anonymous but part of MSElement.  The NG is clear in its purpose and is non-intrusive.  By which I mean that the NG's ID is not part of the element as is the GG value: rather, the NG manager knows about the elements that belong to a particular NG. 

     
    Regards, Jon Summers
    LA Solutions