Convert object to GeoemtryBase in visual studio

HI all,

I am using (trying to use) the DataTree input in Visual studio the first time.
Probably the problem has to do with that I need to convert from the IGH-Goo tree to a geometrybase.

Inside Gh I have a working code but translating it into VS is the problem now.

 protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddBooleanParameter("Execution", "E", "Executes the export process", GH_ParamAccess.item);
            pManager.AddGeometryParameter("Geo", "G", "Geometry to export", GH_ParamAccess.tree);
            pManager.AddTextParameter("Path", "P", "Path to destination folder", GH_ParamAccess.item);
            pManager.AddTextParameter("Format", "F", "Format to export", GH_ParamAccess.item);
            pManager.AddTextParameter("Filename", "F", "Name of the exportfiles", GH_ParamAccess.item);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {



        }

        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object can be used to retrieve data from input parameters and 
        /// to store data in output parameters.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {


            //get Data
            Boolean execute = false;
            if (!DA.GetData(0, ref execute )) return;

            GH_Structure<IGH_Goo> tree = new GH_Structure<IGH_Goo>();
            if ((!DA.GetDataTree(1, out tree))) return;

            String path = null;
            if (!DA.GetData(2, ref path)) return;

            String fileType = null;
            if (!DA.GetData(3, ref fileType)) return;

            String name = null;
            if (!DA.GetData(4, ref name)) return;



          
            //Export 


            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }

            //create filetype

            if (!fileType.StartsWith("."))
            {
                fileType = "." + fileType;
            }


            //declare attributes
            var doc = Rhino.RhinoDoc.ActiveDoc;
            if (doc == null) return;
            var attr = Rhino.RhinoDoc.ActiveDoc.CreateDefaultAttributes();           
      
           if (execute)
                {
                  for (int i = 0; i < tree.PathCount; i++)
                  {
                 
                    for (int j = 0; j < tree.get_Branch(i).Count;j++)
                    {
                        var geos = doc.Objects.Add(tree.get_Branch(i)[j], attr);
                    
                      doc.Objects.Select(geos);
                    }
                    Rhino.RhinoApp.RunScript("-_Export \n\"" + path + "\\" + name + (i).ToString() + fileType + "\"\n _Enter", false);
                    doc.Objects.UnselectAll();
                  }
                     System.Windows.Forms.MessageBox.Show("There were" + tree.PathCount.ToString() + "geometries exported", "Export result", MessageBoxButtons.OK);
                }


         

        }

Any help is much apreciated.

Thanks

Better to use IGH_GeometricGoo rather than GeometryBase

Hey Michael,

thank you for the comment.
I tried that already but the problem remains.
Maybe the error is in another line.

The error messag is:
|Error|1|The best overloaded method match for Rhino.DocObjects.Tables.ObjectTable.Add(Rhino.Geometry.GeometryBase, Rhino.DocObjects.ObjectAttributes)’ has some invalid arguments|

|Error|2|Argument 1: cannot convert from ‘object’ to 'Rhino.Geometry.GeometryBase

in line 109

You can make GeometryBase from GeometricGoo by first casting GeometricGoo to its Rhino Geometry type. So for a curve lets say.

GeometryBase gBase = null;

IGH_GeometricGoo G = null;
DA.GetData(0, ref G);

if (G is GH_Curve)
{
Curve crv = null;
G.CastTo(out crv);
gBase = crv;
}

1 Like

Thanks again Michael,

so if I would like to have it for every type of Geometry I would have to repeat this casting for every case?

I see the advantage of the doing this in the GH script Component is that it is able to cast everything from the geometrybase automatically.

From what I understand if you need to cast from GeometricGoo to GeometryBase yes. The types are these.

GH_Surface
GH_Box
GH_Brep
GH_Mesh
GH_Line
GH_Circle
GH_Arc
GH_Rectangle
GH_Curve

Thanks a lot Michael.

Thats is not as many GeometryClasses as I supected, but when I try it as you suggested I am getting an error:

error
Error 1 ‘Rhino.Geometry.Curve’ does not contain a constructor that takes 1 arguments.

Sorry, I think just get rid of “new”

Thanks Michael ! I tried that now the error says:‘Rhino.Geometry.Curve’ is a ‘type’, which is not valid in the given context

Ah sorry again, I realized what I showed you was how to get a point to be in geometry base since GH_Point isnt a geometry. Curve can just be “gBase = crv;” in the last part. I will edit it above.

Thank you again and dont worry ! Now this part works.

1 Like

Sure thing, just make sure you test things in order. For instance, you should test circle and rectangle before curve. Because a circle and rectangle will output more properties like they do in gh. If you test curve first then a circle or rectangle will just output as a curve (because they are technically a curve also and will be picked up by the curve test and never make it to the circle or rectangle test). Same for surface, brep, and box. Better to test surface first, then box, then brep. The order I wrote above is the usual order I test in.

1 Like

Thanks for all the advices really appreciated. Will try these days to implement all that

There’s lots of handy methods in the GH_Convert class, including one for creating GeometryBase out of almost any known type.

3 Likes

Thanks, that perfect!