Cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted

What's the best way to restore objects visibility and color conditions?

Phenom
Phenom

Hello All!

 

I'm trying to implement commands that acts like "Emphatize Collision Set" and "Show Colliding Pair" commands (commands from Collision Viewer). These commands apply color and visibility filters and after that, can restore the last color and visibility configuration before the filter application.

 

Applying a new color and visibility condition is not a big issue. My problem is basically restoring last color and visibility configuration of graphics viewer objects.

 

For example, restoring visibility is tricky when you have partial visibility objects (because the visibility is actually defined by the inside objects visibility). The possibility of having entity models loaded or not loaded can also be a little tricky because you'll not find those objects inside a TxObjectList, for example.

 

For the colors, is not hard to implement the code, but I'd need to have a list of colors before applying a color filter to all objects and then restore it later, and I don't know if this is a good approach.

 

So, I'd like to know if there's a better and clean approach for restoring objects visibility and color, similar to what those 2 commands ("Emphatize Collision Set" and "Show Colliding Pair") do. Can anybody help?

 

Regards,

4 REPLIES

Re: What's the best way to restore objects visibility and color conditions?

Siemens Phenom Siemens Phenom
Siemens Phenom

Hi Fred,

 

The mechanism used in the collision commands is internal and not accessible.

What I can suggest - try to reload the objects, if they are not many:

TxLocalStorage.ReloadFromLibrary

TxLibraryStorage.Reload

 

Objects will be loaded with the latest saved colors and visibility.

 

Best Regards,

DianaG

Re: What's the best way to restore objects visibility and color conditions?

Phenom
Phenom

Hello @DianaG

 

Thanks for the reply. I'll try to develop something to workaround on that. Loading last saved conditions may not work well every time.

 

Thank you very much!

 

Regards,

Frederico

Re: What's the best way to restore objects visibility and color conditions?

Creator
Creator

I do something similar to the following

 

public static ITxDisplayableObjectEx{

private static bool UpdateChildren(this ITxDisplayableObject displayableObject, bool state)
{
var collection = displayableObject as ITxObjectCollection;
if (collection == null) return true;

var items = collection.GetAllDescendants<ITxDisplayableObject>();
if (items.Any())
return items.All(o => o.SetState(true));

return true;

}
/// <summary>
/// Set Visible state for item
/// </summary>
/// <remarks>
/// Changes Visible state for item and all children
/// </remarks>
/// <param name="displayableObject"></param>
/// <param name="isSelected"></param>
public static bool SetState(this ITxDisplayableObject displayableObject,bool isSelected=false)
{
bool result = true;
// Get All Children First
try
{
result=UpdateChildren(displayableObject, isSelected);

switch (isSelected)
{
case true:
displayableObject.SetVisible();
break;
case false:
displayableObject.SetHidden();
break;
}
}
catch (TxInvalidObjectException ex)
{

}
catch (Exception ex)
{

}

return result;
}

}

Re: What's the best way to restore objects visibility and color conditions?

Phenom
Phenom

Hello @mookie_jones

 

Thanks for your reply. Currently I'm using two methods to do what I was aiming: a "condition collector" and a "condition applier". It's not so efficient, depending on the amount of objects it is possible to feel the processing time, but it is doing the job:

 

Condition Collector: Create an list of array lists containing the condition (color, transparency and visibility) for each obj

public List<ArrayList> GetInitialConditionList(TxObjectList listOfObjs)
        {
            // 1 - Initial condition list: object + isObjectDisplayed + Color + transparecy
            List<ArrayList> masterList = new List<ArrayList>();

            // 2 - Looping through each object
            foreach (ITxDisplayableObject obj in listOfObjs)
            {
                // 2.1 - Checking object visibility
                bool isObjectDisplayed = false;
                if (obj.Visibility == TxDisplayableObjectVisibility.All) // Display object only if is on full visibility
                    isObjectDisplayed = true;
                else if (obj.Visibility == TxDisplayableObjectVisibility.None)
                    isObjectDisplayed = false;
                else if (obj.Visibility == TxDisplayableObjectVisibility.Partial) // Partial visibility is a result of children full and none visibility, so it will be skipped
                    continue;
                
                // 2.2 - Getting object color and transparency (it may return an error)
                try
                {
                    TxColor objColor = obj.Color;
                    double objTransparency = obj.Transparency;
                    
                    // 2.2.1 - Create the arraylist object and add it to the master list
                    ArrayList objProperties = new ArrayList();
                    objProperties.Add(obj);
                    objProperties.Add(isObjectDisplayed);
                    objProperties.Add(objColor);
                    objProperties.Add(objTransparency);
                    masterList.Add(objProperties);
                }
                // 2.3 - If catch error, color and transparency properties were not found, so it will be passed as a boolean
                catch (Exception)
                {
                    bool objColor = false;
                    bool objTransparency = false;

                    // 2.3.1 - Create the arraylist object and add it to the master list
                    ArrayList objProperties = new ArrayList();
                    objProperties.Add(obj);
                    objProperties.Add(isObjectDisplayed);
                    objProperties.Add(objColor);
                    objProperties.Add(objTransparency);
                    masterList.Add(objProperties);
                }
            }

            return masterList;
        }

Condition Applier: Apply the obj condition

public void RestoreInitialCondition(List<ArrayList> masterList)
        {
            foreach (ArrayList list in masterList)
            {
                // 1 - Get the object from the master list
                ITxDisplayableObject obj = list[0] as ITxDisplayableObject;

                // 2 - Check if object was visible or not and display or blank it
                if ((bool)list[1] == true)
                    obj.Display();
                else
                    obj.Blank();

                // 3 - If color = null and type is not a compound operation (otherwise it throws an exception)
                if (list[2] == null && obj.GetType() != typeof(TxCompoundOperation))
                    try { obj.RestoreColor(); }
                    catch (Exception){}

                // 4 - If color is not null, it can be a color obj or a boolean obj
                if (list[2] != null)
                    // 4.1 - If it is a color, apply the color 
                    if (list[2].GetType() == typeof(TxColor))
                    {
                        obj.Color = list[2] as TxColor;
                        obj.Transparency = (double)list[3];

                    }
                    // 4.2 - If it is a boolean, try to restore original color
                    else if (list[2].GetType() == typeof(Boolean))
                    {
                        try { obj.RestoreColor(); }
                        catch (Exception) { }
                    }
            }
        }

Regards