I have an evaluation license to Rhino and I write a plugin in c#.
For some reason I get a com error all the time, even with the basic sample code I downloaded from the forum.
I use windows and the error I get is
DisconnectedContext occurred
Message: Managed Debugging Assistant ‘DisconnectedContext’ has detected a problem in ‘c:\Program Files (x86)\Rhinoceros 5\System\Rhino4.exe’.
Additional information: Transition into COM context 0xc41318 for this RuntimeCallableWrapper failed with the following error: The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED)). This is typically because the COM context 0xc41318 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else. Releasing the interfaces from the current COM context (COM context 0xc41260). This may cause corruption or data loss. To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available for context transition, until the application is completely done with the RuntimeCallableWrappers that represents COM components that live inside them.
Thanks for the answer.
I use VS2015 and I don’t have that specific menu.
When I do go to managed exceptions, I have several options to check / uncheck. for C++, win32 Exception and more.
The odd thing is that if I hit continue several time, it will run. But if I run Rhino (no debug), install the rpc via pluginManager, and run it, it doesn’t work correctly. This simple plugin I did, go over selected object and create some sort of an xml export. When I run it outside of the VS, I will not get the XML file (no errors thought). If I run it via the VS, it will give errors, but with continue it will create the XML.
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;
namespace myPlugin
{
[System.Runtime.InteropServices.Guid("13481eb4-a33c-420a-82b5-21ae0098a124")]
public class createMesh : Command
{
public createMesh()
{
// Rhino only creates one instance of each command class defined in a
// plug-in, so it is safe to store a refence in a static property.
Instance = this;
}
///<summary>The only instance of this command.</summary>
public static createMesh Instance
{
get; private set;
}
///<returns>The command name as it appears on the Rhino command line.</returns>
public override string EnglishName
{
get { return "createMesh"; }
}
public class Segment
{
public double X { get; set; }
public double Y { get; set; }
public double Bulge { get; set; }
public int type()
{
if (Bulge == 0)
return 0;
else
return 1;
}
}
public class Contour
{
[XmlElement("Segment")]
public List<Segment> contour = new List<Segment>();
}
public class Shapes
{
[XmlElement("Contour")]
public List<Contour> contour = new List<Contour>();
}
public static Rhino.Commands.Result GetContour(Rhino.RhinoDoc doc)
{
// TODO: start here modifying the behaviour of your command.
// ---
// Get a single planar closed curve
var go = new Rhino.Input.Custom.GetObject();
go.SetCommandPrompt("Select objects");
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve;
go.GeometryAttributeFilter = Rhino.Input.Custom.GeometryAttributeFilter.ClosedCurve;
go.GroupSelect = true;
go.GetMultiple(1, 0);
if (go.CommandResult() != Rhino.Commands.Result.Success)
return go.CommandResult();
Shapes shapes = new Shapes();
//Shape myContours = new Shape();
for (int i = 0; i < go.ObjectCount; i++)
{
Contour mySegs = new Contour();
var objref = go.Object(i);
var base_curve = objref.Curve();
var first_point = objref.SelectionPoint();
//if (base_curve == null || !first_point.IsValid)
// return Rhino.Commands.Result.Cancel;
Curve[] cr;
//Polyline polyline = new Polyline(point_count);
if (base_curve.GetType().Name == "PolyCurve")
{
cr = base_curve.DuplicateSegments();
}
else
{
PolylineCurve pl = base_curve.ToPolyline(0, 0, .01, 0, 0, 0.01, 0, 0, true);
cr = pl.DuplicateSegments();
}
foreach (var c in cr)
{
if (c.IsLinear())
{
Segment a = new Segment();
a.X = c.PointAtStart.X;
a.Y = c.PointAtStart.Y;
a.Bulge = 0;
mySegs.contour.Add(a);
}
else if (c.IsArc())
{
Segment a = new Segment();
a.X = c.PointAtStart.X;
a.Y = c.PointAtStart.Y;
a.Bulge = ((ArcCurve)c).AngleDegrees;
mySegs.contour.Add(a);
}
}
shapes.contour.Add(mySegs);
}
string path = Directory.GetCurrentDirectory();
string name = "contoursForMesh.xml";
string filePath = Path.Combine(path, name);
XmlSerializer s = new XmlSerializer(typeof(Shapes));
using (FileStream file = File.Create(filePath))
{
s.Serialize(file, shapes);
file.Close();
}
RhinoApp.WriteLine("An XML file was just created at {0}",filePath);
return Rhino.Commands.Result.Success;
}
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// TODO: start here modifying the behaviour of your command.
// ---
RhinoApp.WriteLine("The {0} get an object", EnglishName);
GetContour(doc);
return Result.Success;
}
}
}