# On Model Cutting, I want to get the cut part and the cut part when cutting the model

**URL:** <https://discourse.slicer.org/t/on-model-cutting-i-want-to-get-the-cut-part-and-the-cut-part-when-cutting-the-model/26295>\
**Category:** Development\
**Created:** [November 18, 2022, 4:29am UTC](https://discourse.slicer.org/t/on-model-cutting-i-want-to-get-the-cut-part-and-the-cut-part-when-cutting-the-model/26295 "2022-11-18T04:29:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![miniminic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/miniminic/32/16647_2.png) [@miniminic](https://discourse.slicer.org/u/miniminic)\
**Post date:** [November 18, 2022, 4:29am UTC](https://discourse.slicer.org/t/on-model-cutting-i-want-to-get-the-cut-part-and-the-cut-part-when-cutting-the-model/26295/1 "2022-11-18T04:29:19Z")

</div>

vtkClipPolyData can get two outputs, so at first, I used vtkClipPolyData to cut the model, but the model cut out by vtkClipPolyData was hollow, and the sum of the volumes of the two outputs had a big error with the original model.

So I used vtkClipClosedSurface to cut the model instead, but vtkClipClosedSurface only produced one output. In order to solve the problem of my idea is to use vtkBooleanOperationPolyDataFilter on the original model and the generated model for poor.

But I seem to be stuck in the vtkBooleanOperationPolyDataFilter: : update method.

My code is as follows:

```auto
vtkMRMLModelNode* sourceNode = vtkMRMLModelNode::SafeDownCast(mrmlScene->GetNodeByID(charNodeID));
	if (nullptr != sourceNode)
	{
		this->isClipped = true;
		d->appendButton->setEnabled(false);
		d->deleteButton->setEnabled(true);
		d->clipButton->setEnabled(!this->isClipped);

		
		this->appendPlane();
#if 1
		auto result1 = clipHelp(sourceNode);

		vtkSmartPointer<vtkBooleanOperationPolyDataFilter> boolFilter = vtkSmartPointer<vtkBooleanOperationPolyDataFilter>::New();
		boolFilter->SetOperation(vtkBooleanOperationPolyDataFilter::VTK_DIFFERENCE);

		boolFilter->SetInputData(0, sourceNode->GetPolyData());
		boolFilter->SetInputData(1, result1->GetPolyData());
		boolFilter->Update();	//It's stuck here
		auto result2PolyData = boolFilter->GetOutput();

		vtkSmartPointer<vtkMRMLModelNode> result2 = vtkSmartPointer<vtkMRMLModelNode>::New();
		QString resultName = tr("Result Part_");
		resultName.append(QString::number(this->timeOfClip));
		result2->SetName(resultName.toLatin1().data());
		result2->SetAndObservePolyData(result2PolyData);
		result2->SetScene(mrmlScene);

		vtkSmartPointer<vtkMRMLModelDisplayNode> resultDisplay = vtkSmartPointer<vtkMRMLModelDisplayNode>::New();
		vtkSmartPointer<vtkMRMLModelStorageNode> resultStorage = vtkSmartPointer<vtkMRMLModelStorageNode>::New();
		resultDisplay->SetScene(mrmlScene);
		resultStorage->SetScene(mrmlScene);
		resultDisplay->SetInputPolyDataConnection(result2->GetPolyDataConnection());
		resultStorage->SetFileName(resultName.toLatin1().data());
		mrmlScene->AddNode(resultDisplay);
		mrmlScene->AddNode(resultStorage);
		resultDisplay->SetColor(1.0, 0.0, 0.0);
		result2->SetAndObserveDisplayNodeID(resultDisplay->GetID());
		result2->SetAndObserveStorageNodeID(resultStorage->GetID());
		mrmlScene->AddNode(result2);

		this->deleteSurface();

		//设置在同一分组下
		auto shNode = mrmlScene->GetSubjectHierarchyNode();
		if (nullptr != shNode)
		{
			auto groupName = std::string(sourceNode->GetName()) + "_Clip";
			auto parent = shNode->GetItemByName(groupName);
			if (0 == parent)
				parent = mrmlScene->GetSubjectHierarchyNode()->CreateFolderItem(mrmlScene->GetSubjectHierarchyNode()->GetSceneItemID(), groupName);

			if (0 != parent && nullptr != result1)
				mrmlScene->GetSubjectHierarchyNode()->SetItemParent(mrmlScene->GetSubjectHierarchyNode()->GetItemByDataNode(result1), parent);

			if (0 != parent && nullptr != result2)
				mrmlScene->GetSubjectHierarchyNode()->SetItemParent(mrmlScene->GetSubjectHierarchyNode()->GetItemByDataNode(result2), parent);
		}

		auto sourceDisplayNode = sourceNode->GetModelDisplayNode();
		//隐藏被剪切的model
		if (nullptr != sourceDisplayNode)
			sourceDisplayNode->SetVisibility(false);

```

Can someone help me, or do I have a better way to get two closed cut solid models and cut solid models after cutting models?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 19, 2022, 5:39pm UTC](https://discourse.slicer.org/t/on-model-cutting-i-want-to-get-the-cut-part-and-the-cut-part-when-cutting-the-model/26295/2 "2022-11-19T17:39:14Z")

</div>

vtkBooleanOperationPolyDataFilter does not work: it fails randomly for completely valid inputs, therefore I would not recommend using that.

Instead, you can use the much more robust Combine Models module in Sandbox extension.

---

<div class="post-metadata">

**Author:** ![miniminic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/miniminic/32/16647_2.png) [@miniminic](https://discourse.slicer.org/u/miniminic)\
**Post date:** [November 20, 2022, 2:16am UTC](https://discourse.slicer.org/t/on-model-cutting-i-want-to-get-the-cut-part-and-the-cut-part-when-cutting-the-model/26295/3 "2022-11-20T02:16:18Z")

</div>

Thank you. I’ll try this extension

---

<div class="post-metadata">

**Author:** ![miniminic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/miniminic/32/16647_2.png) [@miniminic](https://discourse.slicer.org/u/miniminic)\
**Post date:** [November 22, 2022, 3:10am UTC](https://discourse.slicer.org/t/on-model-cutting-i-want-to-get-the-cut-part-and-the-cut-part-when-cutting-the-model/26295/4 "2022-11-22T03:10:46Z")

</div>

Sorry to bother you, I didn’t find Combine Models in the extension module, where do I get it

 ![屏幕截图 2022-11-22 111028](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/2/82c01bc313528a44f4087d6ed5fca182c10b6314.jpeg)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 22, 2022, 3:13am UTC](https://discourse.slicer.org/t/on-model-cutting-i-want-to-get-the-cut-part-and-the-cut-part-when-cutting-the-model/26295/5 "2022-11-22T03:13:52Z")

</div>

The extension’s name is `Sandbox`. You will find `Combine models` module in the module finder after you installed the extension.

---

<div class="post-metadata">

**Author:** ![miniminic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/miniminic/32/16647_2.png) [@miniminic](https://discourse.slicer.org/u/miniminic)\
**Post date:** [November 22, 2022, 3:21am UTC](https://discourse.slicer.org/t/on-model-cutting-i-want-to-get-the-cut-part-and-the-cut-part-when-cutting-the-model/26295/6 "2022-11-22T03:21:50Z")

</div>

All right, thank you very much
