algorithmic modeling for Rhino

Hi all,

I have a transformation matrix from another program that I would like to import into Grasshopper and apply as an xform. Can someone shed some light on how to create a transformation matrix that can be used as the xform in an rs.TransformObject(id, xform) operation?


Views: 1059

Replies to This Discussion

The type Rhino.Geometry.Transform will be converted into Grasshopper transform data. So you just need to figure out how to put the numbers from your external transform matrix into a Rhino transform matrix.


David Rutten

Poprad, Slovakia

Thanks David. Is there an example that shows how to do this in Python?

have you found a solution?

Maybe this could help...

But I think you will still need casting in some cases.


This is a super late question, but what is casting? 

Casting typically means changing the type of a variable. This is only possible when an object is actually of the new type. This only makes sense in a type-safe language.

For example, imagine we have a type hierarchy with two interfaces and three classes:

  • IVehicle
  • IAutomobile
  • Bicycle
  • VolvoCar
  • AudiCar

IVehicle is an interface which lays down some ground-rules for all sort of vehicles. For example their maximum speed, maximum number of occupants and fuel type (diesel, horse, foot-power).

Bicycle is a class which implement IVehicle and nothing else.

IAutomobile is another interface, which derives from IVehicle but adds some more rules. For example it also demands that any class implementing IAutomobile has a property telling you whether the steering wheel is on the left or the right and how many kilometers per litre of fuel it manages.

VolvoCar and AudiCar are classes that both implement IAutomobile (they are, after all, both a type of automobile). Since IAutomobile inherits from IVehicle, they need to also implement that interface. VolvoCar might look like this:

class VolvoCar : IAutomobile


  public int MaxSpeed { get { return 160; } } //IVehicle implementation

  public int MaxPeople { get { return 5; } }    //IVehicle implementation

  public string Fuel { get { return "diesel"; } } //IVehicle implementation

  public double Consumption { get return "18.4"; } } //IAutomobile implementation

  public bool WheelOnLeft { get { return true; } } //IAutomobile implementation

  public string TypeCode { get { return "V70"; } } //A property only volvos have.


Now let's imagine that you are given an instance of VolvoCar. This can happen as one of four types:

  • System.Object (everything derives from System.Object, so this is always possible)
  • IVehicle (VolvoCar implements this interface because IAutomobile derives from it)
  • IAutomobile (VolvoCar specifically implements this interface)
  • VolvoCar (the type itself)

These are just four different ways of looking at VolvoCar, and each way allows you to see less or more detail. If you have a VolvoCar instance stored inside a variable of type IVehicle, then you do not have access to the steering wheel property, because that is not part of IVehicle. If you want to access WheelOnLeft or Consumption you can cast your instance to type IAutomobile:

Dim vehicle As IVehicle = GetVehicleFromSomewhere()

Dim car As IAutomobile = DirectCast(vehicle, IAutomobile)

The car and vehicle variables now both point to the same volvo in memory, but car allows you to access more information. You could even cast it to a VolvoCar, in order to get access not just to WheelOnLeft and Consumption, but also to TypeCode:

Dim vehicle As IVehicle = GetVehicleFromSomewhere()

Dim volvo As VolvoCar = DirectCast(vehicle, VolvoCar)

Note though that casting sort of violates the type-safetyness of a program. The GetVehicleFromSomewhere() function returns a variable of type IVehicle. So it is allowed to return VolvoCar, AudiCar or Bicycle without breaking that promise. But if it returns Bicycle then our attempt to cast it to an IAutomobile or VolvoCar will fail.

So, conclusion:

  • Casting allows you to look at an instance using a more detailed (or less detailed if you want) type. Pre-requisite is that the instance actually is of that type.
  • In VB.NET, you can use the DirectCast and TryCast methods for this. TryCast is to be preferred, but it only works on classes, not structs.
  • In C# you can use the (type)variable notation or the as keyword.
  • You can use the TypeOf (VB.NET) and is (C#) keywords to first see whether a cast will work or not.


David Rutten

Tirol, Austria

Wow. Thanks David! Much appreciated. 

Hi David,

This is me thinking out loud:

How can one find out what casts are associated to an instance?

For example, using xform on a GUID of a brep. Can I recast this GUID to perform brep methods on the same instance? 

I will try this and share my findings....


Nope. A Guid is 16 bytes worth of unique nonsense that allows you to identify pretty much anything in a non-ambiguous way. A Brep is an incredibly complicated geometric and topological database representing a number of surfaces with trimming curves, edges, vertices and loops. There's no way on Gods Green Earth you'll be able to cram any Brep, no matter how simple, into 16 bytes.

A Guid might point to a Brep in the Rhino document, but it certainly isn't castable to one.

It isn't particularly easy to figure out all the things you can cast something to. If you're using Visual Studio .NET then you can use the debugger to look at a variable and see the most detailed type. If you're using VB/C# components then you can use the Reflect() method and pass in any variable. It will then write detailed information about it to the [out] parameter.


David Rutten

Tirol, Austria

Thanks again David. I was trying to figure out why it wasn't working.

Right now, all this is for a problem that has me stumped at step 1... I am trying to simply make a mesh box orient itself toward a point using transformations/change of basis in Csharp. 

The ambition is to learn to do relative transformations (scale, move, rotate) and cobble together a simple L-system. 

I'd love your two cents on whats wrong here. 



[reposted to new topic in forum as it is a deviation from this threads topic...]






  • Add Photos
  • View All

© 2020   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service