ChronoEngine_SwAddin.SWTaskpaneHost.PythonTraverseComponent_for_visualizationshapes C# (CSharp) Method

PythonTraverseComponent_for_visualizationshapes() public method

public PythonTraverseComponent_for_visualizationshapes ( Component2 swComp, long nLevel, string &asciitext, int nbody, int &nvisshape, Component2 chbody_comp ) : void
swComp Component2
nLevel long
asciitext string
nbody int
nvisshape int
chbody_comp Component2
return void
        public void PythonTraverseComponent_for_visualizationshapes(Component2 swComp, long nLevel, ref string asciitext, int nbody, ref int nvisshape, Component2 chbody_comp)
        {
            CultureInfo bz = new CultureInfo("en-BZ");
            object[] bodies;
            object bodyInfo;
            bodies = (object[])swComp.GetBodies3((int)swBodyType_e.swAllBodies, out bodyInfo);

            if (bodies != null)
              if (bodies.Length > 0)
            {
                // Export the component shape to a .OBJ file representing its SW body(s)
                nvisshape += 1;
                string bodyname  = "body_" + nbody;
                string shapename = "body_" + nbody + "_" + nvisshape;
                string obj_filename = this.save_dir_shapes + "\\" + shapename + ".obj";

                ModelDoc2 swCompModel = (ModelDoc2)swComp.GetModelDoc();
                if (!this.saved_shapes.ContainsKey(swCompModel.GetPathName()))
                {
                    try
                    {
                        FileStream ostream = new FileStream(obj_filename, FileMode.Create, FileAccess.ReadWrite);
                        StreamWriter writer = new StreamWriter(ostream); //, new UnicodeEncoding());
                        string asciiobj = "";
                        if (this.swProgress != null)
                            this.swProgress.UpdateTitle("Exporting " + swComp.Name2 + " (tesselate) ...");
                        // Write the OBJ converted visualization shapes:
                        TesselateToObj.Convert(swComp, ref asciiobj, this.checkBox_saveUV.Checked, ref this.swProgress);
                        writer.Write(asciiobj);
                        writer.Flush();
                        ostream.Close();

                        this.saved_shapes.Add(swCompModel.GetPathName(), shapename);
                    }
                    catch (Exception)
                    {
                        System.Windows.Forms.MessageBox.Show("Cannot write to file: " + obj_filename);
                    }
                }
                else
                {
                    // reuse the already-saved shape name
                    shapename = (String)saved_shapes[swCompModel.GetPathName()];
                }

                asciitext += String.Format(bz, "\n# Visualization shape \n", bodyname);
                asciitext += String.Format(bz, "{0}_shape = chrono.ChObjShapeFile() \n", shapename);
                asciitext += String.Format(bz, "{0}_shape.SetFilename(shapes_dir +'{0}.obj') \n", shapename);

                object foo = null;
                double[] vMatProperties = (double[])swComp.GetMaterialPropertyValues2((int)swInConfigurationOpts_e.swThisConfiguration, foo);

                if (vMatProperties != null)
                    if (vMatProperties[0] != -1)
                    {
                        asciitext += String.Format(bz, "{0}_shape.SetColor(chrono.ChColor({1},{2},{3})) \n", shapename,
                            vMatProperties[0], vMatProperties[1], vMatProperties[2]);
                        asciitext += String.Format(bz, "{0}_shape.SetFading({1}) \n", shapename, vMatProperties[7]);
                    }

                MathTransform absframe_chbody = chbody_comp.GetTotalTransform(true);
                MathTransform absframe_shape  = swComp.GetTotalTransform(true);
                MathTransform absframe_chbody_inv = absframe_chbody.IInverse();
                MathTransform relframe_shape = absframe_shape.IMultiply(absframe_chbody_inv);  // row-ordered transf. -> reverse mult.order!
                double[] amatr = (double[])relframe_shape.ArrayData;
                double[] quat  = GetQuaternionFromMatrix(ref relframe_shape);

                asciitext += String.Format(bz, "{0}_level = chrono.ChAssetLevel() \n", shapename);
                asciitext += String.Format(bz, "{0}_level.GetFrame().SetPos(chrono.ChVectorD({1},{2},{3})) \n", shapename,
                    amatr[9] *ChScale.L,
                    amatr[10]*ChScale.L,
                    amatr[11]*ChScale.L);
                asciitext += String.Format(bz, "{0}_level.GetFrame().SetRot(chrono.ChQuaternionD({1},{2},{3},{4})) \n", shapename, quat[0], quat[1], quat[2], quat[3]);
                asciitext += String.Format(bz, "{0}_level.GetAssets().push_back({0}_shape) \n", shapename);

                asciitext += String.Format(bz, "{0}.GetAssets().push_back({1}_level) \n", bodyname, shapename);

            }

            // Recursive scan of subcomponents

            Component2 swChildComp;
            object[] vChildComp = (object[])swComp.GetChildren();

            for (long i = 0; i < vChildComp.Length; i++)
            {
                swChildComp = (Component2)vChildComp[i];

                if (swChildComp.Visible == (int)swComponentVisibilityState_e.swComponentVisible)
                    PythonTraverseComponent_for_visualizationshapes(swChildComp, nLevel + 1, ref asciitext, nbody, ref nvisshape, chbody_comp);
            }
        }