Site Tools


Modify an existing Flamingo nXt plant

Developer: FlamingoScript
Summary: Demonstrates how to modify an existing Flamingo nXt plant.

Question

I am trying to figure out how to modify an existing Flamingo nXt plant. What can I do?

Answer

The following are examples of how to modify an existing Flamingo nXt plant so that it has 3 trunks.

RhinoScript

Option Explicit
 
Call Main()
 
Sub Main()
	Dim objPlugIn, strObject, plant, xml, xmlDoc, node
	On Error Resume Next
	Set objPlugIn = Rhino.GetPluginObject("8008880f-8d13-4b2d-92b0-727e12878a4c")
	If Err Then
		MsgBox Err.Description
		Exit Sub
	End If
	strObject = Rhino.GetObject("Select object")
  If Not IsNull(strObject) And Not strObject = "" Then
		Set plant = objPlugIn.PlantFromObject(strObject)
    If IsNull(plant) Then
      Rhino.Print("Object is not a plant")
    Else
      xml = plant.XmlWithoutHeader
      If IsNull(xml) Then
        Rhino.Print("Error extracting plant XML")
      Else
        Set xmlDoc = CreateObject("Microsoft.XMLDOM")
        If IsNull(xmlDoc) Then
          Rhino.Print("Error creating XML document")
        Else
          xmlDoc.LoadXml(xml)
          Set node = xmlDoc.SelectSingleNode("ArPlantDef/PlantDef/nTrunks")
          If IsNull(node) Or IsEmpty(node) Then
            Rhino.Print("Error getting plant XML")
          Else
            'Mofiy the trunk value changing it to 3 trunks
            node.Text = "3"
            If objPlugIn.ModifyPlantObject(strObject, xmlDoc.xml) Then
              Rhino.Print("Plant now has three trunks...")
            Else
              Rhino.Print("Error modifing plant...")
            End If
          End If
        End If
      End If
    End If
	End If
  Set plant = Nothing
	Set objPlugIn = Nothing
End Sub

Python

import clr
# Load the Flamingo nXt SDK DLL
clr.AddReference("FlamingoNXtSDK")
clr.AddReference("System.Xml")
import FlamingoNXtSDK
import System.Xml
import rhinoscriptsyntax as rs
import scriptcontext
# Get the Flamingo nXt plug-in's SDK implimentaion, this will force the
# plug-in to load if it is not currently loaded
flSDK = FlamingoNXtSDK.SDK.FlamingoSDK
 
def ChangePlantTo3Trunks():
    objID = rs.GetObject("Select plant object")
    if not objID: return
    sdkPlant = flSDK.PlantFromObject(objID)
    if not sdkPlant: return
    xmlDoc = sdkPlant.ToXmlDocument()
    if None == xmlDoc: return
    node = xmlDoc.SelectSingleNode("ArPlantDef/PlantDef/nTrunks")
    if not node: return
    node.InnerText = "3"
    modifiedXML = FlamingoNXtSDK.Plant.XmlDocumentToString(xmlDoc)
    if flSDK.ModifyPlantObject(objID, modifiedXML):
        print "Plant now has three trunks..."
 
if __name__=="__main__":
    ChangePlantTo3Trunks()

C#

using System;
using System.Xml;
 
using Rhino;
using Rhino.DocObjects;
using Rhino.Input;
using Rhino.Commands;
 
using FlamingoNXtSDK;
 
partial class Examples
{
  public static Result ChangeTo3Trunks(RhinoDoc doc, Rhino.Commands.RunMode mode)
  {
    SDK sdk = SDK.FlamingoSDK;
    if (null == sdk)
      return Rhino.Commands.Result.Failure;
    ObjRef objRef = null;
    Result result = RhinoGet.GetOneObject("Select plant object", false, ObjectType.None, out objRef);
    if (result == Result.Success && null != objRef)
    {
      Plant sdkPlant = sdk.PlantFromObject(objRef.ObjectId);
      if (null != sdkPlant)
      {
        try
        {
          XmlDocument xmlDoc = sdkPlant.ToXmlDocument();
          XmlNode node = xmlDoc.SelectSingleNode("ArPlantDef/PlantDef/nTrunks");
          if (null != node)
          {
            node.InnerText = "3";
            string modifiedXML = Plant.XmlDocumentToString(xmlDoc);
            if (string.IsNullOrEmpty(modifiedXML) || !sdk.ModifyPlantObject(objRef.ObjectId, modifiedXML))
              result = Result.Failure;
          }
        }
        catch (Exception ex)
        {
          RhinoApp.WriteLine("Exception: \"" + ex.Message + "\"" + Environment.NewLine + ex.StackTrace);
        }
        if (result == Result.Success)
          RhinoApp.WriteLine("Plant now has three trunks...");
      }
    }
    return result;
  }
}
flamingo/flamingosdk/modifyplant.txt · Last modified: 2012/04/19 by johnm