Painting Setting

I want to trigger the painting setting through the code in the following code, but it fails to execute properly and shows an error. Where did I make a mistake? Thank you.

        private void PrpareForPainting_click(object sender, RoutedEventArgs e)
        {

            ICommandRegistry cmd = IoC.Get<ICommandRegistry>();
            IActionItem extractCmd = cmd.FindItem("PrpareForPainting");
            extractCmd.Execute();
        }

Error : System.NullReferenceException:“Object reference not set to an instance of an object.”

That command takes a bool parameter, and only opens or closes the UI for it. I think there is no API to prepare components for painting programmatically.

So there is no way to set the Prepare Selected Components method?
Thank you

What the “Prepare for painting” does is collect all triangle meshes (ITriangleSet) from all node’s geometry (ISimNode.Geometry) of the selected component and then call

ITriangleSet.SubdivideMesh(MaxEdgeLength, int.MaxValue);
ITriangleSet.ResetTextureCoordinateValues();

Then it sets (or creates) a boolean property in the component called “PaintPrepared” to true.

So, even though you can not use the command directly, you can do the same using the .NET API.

I’m not sure how to use it. Can you give me some examples?
Thanks

Something like this (did not test it):

private void PrpareForPainting_click(object sender, RoutedEventArgs e)
{
    var c = IoC.Get<ISelectionManager>().GetSelected<ISimComponent>();
    double maxEdgeLength = 50;
    SubvideMesh(c.RootNode);

	var componentPreparedProp = c.Properties.FirstOrDefault(p => p.Name == PaintProperties.PAINT_PREPARED);
	if (componentPreparedProp == null)
	{
		componentPreparedProp = c.CreateProperty(typeof(bool), PropertyConstraintType.NotSpecified);
		componentPreparedProp.Name = PaintProperties.PAINT_PREPARED;
		componentPreparedProp.IsVisible = false;
	}
	componentPreparedProp.Value = true;

	IoC.Get<IPaintColorMapsEditorViewModel>().AddMaterialToComponent(c, true, true);

}

private void SubvideMesh(ISimNode node, double maxEdgeLength)
{
    var sets = node.Geometry.GeometrySets.OfType<ITriangleSet>();
    foreach (var triangleset in sets)
    {
        NotificationBar();
        triangleset.SubdivideMesh(maxEdgeLength, int.MaxValue);
        triangleset.ResetTextureCoordinateValues();
    }

    var children = node.Children;
    foreach (var n in children)
    {
        SubvideMesh(n, maxEdgeLength);
    }
}

Thank you, I have tested it and it works