Intersection of two surface

Hi All

// cmdPlugIn1.cpp : command file
//

#include "StdAfx.h"


////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// BEGIN PlugIn1 command
//

#pragma region PlugIn1 command

class CCommandPlugIn1 : public CRhinoCommand
{
public:
   CCommandPlugIn1() = default;
  ~CCommandPlugIn1() = default;
  UUID CommandUUID() override
  {
    // {667F5989-879F-4BFE-9FB3-2969D13BEED9}
    static const GUID PlugIn1Command_UUID =
    { 0x667F5989, 0x879F, 0x4BFE, { 0x9F, 0xB3, 0x29, 0x69, 0xD1, 0x3B, 0xEE, 0xD9 } };
    return PlugIn1Command_UUID;
  }

  const wchar_t* EnglishCommandName() override { return L"PlugIn1"; }
  CRhinoCommand::result RunCommand(const CRhinoCommandContext& context) override;
};

static class CCommandPlugIn1 thePlugIn1Command;

CRhinoCommand::result CCommandPlugIn1::RunCommand(const CRhinoCommandContext& context)
{
	CRhinoGetObject go0;
	go0.SetCommandPrompt(L"1Select surfaces or polysurfaces");
	go0.SetGeometryFilter(CRhinoGetObject::surface_object | CRhinoGetObject::polysrf_object);
	go0.EnableSubObjectSelect(FALSE);
	go0.GetObjects(1, 0);
	if (go0.CommandResult() != CRhinoCommand::success)
		return go0.CommandResult();

	const int InBreps0_count = go0.ObjectCount();
	ON_SimpleArray<const CRhinoObject*> InBreps0_objects(InBreps0_count);
	ON_SimpleArray<const ON_Brep*> InBreps0(InBreps0_count);

	for (int i = 0; i < InBreps0_count; i++)
	{
		const CRhinoObject* obj = go0.Object(i).Object();
		const ON_Brep* brep = go0.Object(i).Brep();

		if (0 == obj || 0 == brep)
			return CRhinoCommand::failure;

		InBreps0_objects.Append(obj);
		InBreps0.Append(brep);
	}
	CRhinoGetObject go1;
	go1.SetCommandPrompt(L"2 Select surfaces or polysurfaces");
	go1.SetGeometryFilter(CRhinoGetObject::surface_object | CRhinoGetObject::polysrf_object);
	go1.EnableSubObjectSelect(FALSE);
	go1.EnablePreSelect(FALSE, TRUE);
	go1.EnableDeselectAllBeforePostSelect(false);
	go1.GetObjects(1, 0);
	if (go1.CommandResult() != CRhinoCommand::success)
		return go1.CommandResult();

	const int InBreps1_count = go1.ObjectCount();
	ON_SimpleArray<const CRhinoObject*> InBreps1_objects(InBreps1_count);
	ON_SimpleArray<const ON_Brep*> InBreps1(InBreps1_count);

	for (int i = 0; i < InBreps1_count; i++)
	{
		const CRhinoObject* obj = go1.Object(i).Object();
		const ON_Brep* brep = go1.Object(i).Brep();
		if (0 == obj || 0 == brep)
			return CRhinoCommand::failure;

		InBreps1_objects.Append(obj);
		InBreps1.Append(brep);
	}

	double tol = context.m_doc.AbsoluteTolerance();
	bool something_happened = false;
	ON_SimpleArray<ON_Brep*> OutBreps;
	ON_SimpleArray<int> InputIndexForOutput;

	CWaitCursor wait;
	bool rc = RhinoBooleanIntersection(InBreps0, InBreps1, tol, &something_happened, OutBreps);
	wait.Restore();

	if (!rc || !something_happened)
	{
		for (int i = 0; i < OutBreps.Count(); i++)
			delete OutBreps[i];

		RhinoApp().Print(L"Boolean intersection failed.\n");
		return CRhinoCommand::failure;
	}

	for (int i = 0; i < OutBreps.Count(); i++)
	{
		CRhinoBrepObject* brep_obj = new CRhinoBrepObject();
		brep_obj->SetBrep(OutBreps[i]);

		context.m_doc.AddObject(brep_obj);
		OutBreps[i] = 0;
	}

	for (int i = 0; i < InBreps0_count; i++)
		context.m_doc.DeleteObject(go0.Object(i));

	for (int i = 0; i < InBreps1_count; i++)
		context.m_doc.DeleteObject(go1.Object(i));

	context.m_doc.Redraw();
  return CRhinoCommand::success;
}

#pragma endregion

//
// END PlugIn1 command
//
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////

Moved to Scripting category so it will be seen

Hi @ccx07,

Use RhinoIntersectBreps. See rhinoSdkUtilities.h for more details.

– Dale