Boolean difference - face parent

Hi,
I’m trying to apply a boolean difference. My inputs are a face (I am trying to get its parent closed polysurface) and an extrusion

Dim SelectedFace As Rhino.DocObjects.ObjRef
Dim MyExtrusion As Rhino.DocObjects.RhinoObject

I tried to use this example:


but it requires to find my face’s parent and to cast my rhinoObject as an objtref and I don’t know how to to that…

Can you provide that code that you are working on so we can see how you’ve gotten to where you are at?

Sure. My macro is supposed to make a hole centered on a volume border. I pick a face and a point, then I import some blocks from a file, I extrude them, and next I just need to do a boolean difference.

Here is the code:

'Vérifier existence du fichier de profils
Dim FileProfilWindow As New Windows.Forms.OpenFileDialog
FileProfilWindow.CheckFileExists = True
FileProfilWindow.Multiselect = False
FileProfilWindow.Title = "Choose the Cuto library 3dm file"
FileProfilWindow.InitialDirectory = My.Computer.FileSystem.GetFileInfo(My.Settings.FilePath).DirectoryName
FileProfilWindow.FileName = My.Settings.FilePath
FileProfilWindow.ShowDialog()
My.Settings.FilePath = FileProfilWindow.FileName

        'Importer les blocs
        Rhino.RhinoApp.RunScript("-_Import """ & My.Settings.FilePath & """ ENTER", True)

        'instancier la fenetre de selection
        Dim SelectionCutoBox As New ListSelector
        SelectionCutoBox.ListCuto.SelectionMode = Windows.Forms.SelectionMode.One
        SelectionCutoBox.ShowInTaskbar = False
        SelectionCutoBox.ShowIcon = False
        SelectionCutoBox.Text = "Please select a Cuto"

        'Lire les profils disponibles (blocs)
        Dim TpProfilNames As New ArrayList
        RhinoApp.WriteLine("Reading the Profile library")
        For i = 0 To doc.InstanceDefinitions.Count - 1
            If doc.InstanceDefinitions.Item(i).Name.ToUpper.Contains("CUTO") Then
                SelectionCutoBox.ListCuto.Items.Add(doc.InstanceDefinitions.Item(i).Name)
            End If
        Next

        'Choix de la surface
        Dim SelectedFace As Rhino.DocObjects.ObjRef = Nothing
        RhinoGet.GetOneObject("Select Base Surface", False, DocObjects.ObjectType.Surface, SelectedFace)
        Try
            SelectedFace.Face()
        Catch ex As Exception
            RhinoApp.WriteLine("Selection invalide")
            Return Result.Nothing
        End Try

        'Extraction de la bordure
        Dim SelectedFaceBrep As Rhino.Geometry.Brep
        Dim FinalEdgeCurve() As Rhino.Geometry.Curve
        If SelectedFace.Geometry.HasBrepForm Then
            SelectedFaceBrep = Brep.TryConvertBrep(SelectedFace.Face.DuplicateFace(False))
            'doc.Objects.Select(SelectedFace)
            Dim nakedEdges As Curve() = SelectedFaceBrep.DuplicateNakedEdgeCurves(True, False)
            FinalEdgeCurve = Curve.JoinCurves(nakedEdges, 2.1 * doc.ModelAbsoluteTolerance)
            'doc.Objects.AddCurve(FinalEdgeCurve(0)) 'dessiner la courbe
        Else
            MsgBox("Error - NoBrepForm")
            Return Result.Failure
        End If

        'Orientation par défaut
        Dim OrientationSelectedIndex As Integer = 0

        'Selection du point
        Dim SelectedPoint As Rhino.Geometry.Point3d ' point d'insertion
        Dim gp As New Rhino.Input.Custom.GetPoint
        gp.Constrain(FinalEdgeCurve(0), False)
        gp.SetCommandPrompt("Select base point")
        While True
            Dim get_rc As Rhino.Input.GetResult = gp.Get()
            If gp.CommandResult() <> Rhino.Commands.Result.Success Then
                Return gp.CommandResult()
            End If
            If get_rc = Rhino.Input.GetResult.Point Then
                SelectedPoint = gp.Point
                Exit While
            End If
        End While

        ' Calculer la normale au plan
        Dim PlanNormalVector As New Rhino.Geometry.Vector3d
        Dim u As Double = 0.0, v As Double = 0.0
        If SelectedFace.Face.ClosestPoint(SelectedPoint, u, v) Then
            PlanNormalVector = SelectedFace.Face.NormalAt(u, v)
            'RhinoApp.WriteLine("Normal Vector")
            'RhinoApp.WriteLine(PlanNormalVector.X)
            'RhinoApp.WriteLine(PlanNormalVector.Y)
            'RhinoApp.WriteLine(PlanNormalVector.Z)
        End If

        'Calculer la tangente à la bordure de surface
        Dim PointEdgeT As Double
        Dim CurveTangentVector As New Rhino.Geometry.Vector3d
        FinalEdgeCurve(0).ClosestPoint(SelectedPoint, PointEdgeT)
        CurveTangentVector = FinalEdgeCurve(0).TangentAt(PointEdgeT)

        'définition des vecteurs world
        Dim WorldZvector As New Rhino.Geometry.Vector3d
        WorldZvector.X = 0
        WorldZvector.Y = 0
        WorldZvector.Z = 1
        Dim WorldXvector As New Rhino.Geometry.Vector3d
        WorldXvector.X = 1
        WorldXvector.Y = 0
        WorldXvector.Z = 0
        Dim WorldYvector As New Rhino.Geometry.Vector3d
        WorldXvector.X = 0
        WorldXvector.Y = 1
        WorldXvector.Z = 0

        'Cplane final
        Dim plane As New Rhino.Geometry.Plane(SelectedPoint, CurveTangentVector, PlanNormalVector)

        'Choix du cuto
        SelectionCutoBox.ShowDialog()
        Dim blocDefinitionIndex As Integer = doc.InstanceDefinitions.Find(SelectionCutoBox.ListCuto.SelectedItem.ToString(), True).Index()


        'Insérer le bloc
        Dim MyTranslate As Rhino.Geometry.Transform
        MyTranslate = Rhino.Geometry.Transform.Translation(SelectedPoint.X, SelectedPoint.Y, SelectedPoint.Z)
        Dim BlocID As Guid = doc.Objects.AddInstanceObject(blocDefinitionIndex, MyTranslate)
        doc.Views.Redraw()
        Dim MyBlocInstance As Rhino.DocObjects.InstanceObject = doc.Objects.Find(BlocID)
        'Rotation pour alignement 
        Dim MyTransform As Rhino.Geometry.Transform
        MyTransform = Rhino.Geometry.Transform.Rotation(WorldXvector, plane.Normal, SelectedPoint)
        BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
        MyBlocInstance.CommitChanges()
        doc.Views.Redraw()
        MyBlocInstance = doc.Objects.Find(BlocID)
        MyTransform = Rhino.Geometry.Transform.Rotation(Math.PI / 2, plane.Normal, SelectedPoint) 'rotation de 90° suivant CurveTangentVector
        BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
        MyBlocInstance.CommitChanges()
        doc.Views.Redraw()
        MyBlocInstance = doc.Objects.Find(BlocID)
        MyTransform = Rhino.Geometry.Transform.Rotation(-Math.PI, CurveTangentVector, SelectedPoint) 'rotation de 90° suivant CurveTangentVector
        BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
        MyBlocInstance.CommitChanges()
        doc.Views.Redraw()


        'CHOIX DES OPTIONS
        Dim GetOpt As New Rhino.Input.Custom.GetOption
        GetOpt.SetCommandPrompt("Cuto Options")

        'Option d'inversion
        Dim Inverser As New Rhino.Input.Custom.OptionToggle(True, "No", "Yes")
        GetOpt.AddOptionToggle("Invert", Inverser)
        Dim InverserLastValue As Boolean = Inverser.InitialValue

        'Type de Cuto
        Dim ChangeCuto As New Rhino.Input.Custom.OptionToggle(True, "ChangeCuto", "ChangeCuto")
        Dim ChangeCutoLastValue As Boolean = ChangeCuto.InitialValue

        'Option d'orientation
        Dim OrientationOptions As String() = New String() {"Tangent", "Horizontal", "Vertical"}
        Dim OrientationList As Integer = GetOpt.AddOptionList("Orientation", OrientationOptions, OrientationSelectedIndex)
        Dim OrientationLastValue As String = "Tangent"


        While True
            Dim get_rc As Rhino.Input.GetResult = GetOpt.Get()
            If get_rc = Rhino.Input.GetResult.Option Then
                If GetOpt.OptionIndex() = OrientationList Then
                    OrientationSelectedIndex = GetOpt.Option().CurrentListOptionIndex
                End If
                Rhino.RhinoApp.WriteLine("Command line option values are")
                Rhino.RhinoApp.WriteLine("Invert = {0}", Inverser.CurrentValue)
                'Rhino.RhinoApp.WriteLine("Cuto's Name = {0}", ProfilNames(listCutoSelectedIndex))
                Rhino.RhinoApp.WriteLine("Orientation = {0}", OrientationOptions(OrientationSelectedIndex))

                'correction de vecteur pour l'orientation horizontale
                '(bug du cuto vers l'exterieur quand on selectionne une face orientée vers l'arrière)
                Dim CorrectedTangentVector As Rhino.Geometry.Vector3d = CurveTangentVector
                Rhino.RhinoApp.WriteLine(CurveTangentVector.X)
                If (CurveTangentVector.X > 0 And WorldZvector.X > 0) Or (CurveTangentVector.X < 0 And WorldZvector.X < 0) Then
                    CorrectedTangentVector = Rhino.Geometry.Vector3d.Negate(CurveTangentVector)
                Else
                    CorrectedTangentVector = CurveTangentVector
                End If

                'Choix du cuto
                If ChangeCuto.CurrentValue <> ChangeCutoLastValue Then
                    SelectionCutoBox.ShowDialog()
                    ChangeCutoLastValue = ChangeCuto.CurrentValue
                End If

                'Gérer le changement d'orientation
                If GetOpt.Option.EnglishName = "Orientation" Then
                    'MsgBox(OrientationOptions(OrientationSelectedIndex)) 'Option sélectionnée
                    If OrientationLastValue <> OrientationOptions(OrientationSelectedIndex) Then 'Si l'orientation sélectionnée à changé
                        If OrientationLastValue = "Tangent" And OrientationOptions(OrientationSelectedIndex) = "Vertical" Then
                            MyBlocInstance = doc.Objects.Find(BlocID)
                            MyTransform = Rhino.Geometry.Transform.Rotation(CurveTangentVector, WorldXvector, SelectedPoint) 'rotation de vers Z
                            BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
                            MyBlocInstance.CommitChanges()
                            doc.Views.Redraw()
                        ElseIf OrientationLastValue = "Vertical" And OrientationOptions(OrientationSelectedIndex) = "Tangent" Then
                            MyBlocInstance = doc.Objects.Find(BlocID)
                            MyTransform = Rhino.Geometry.Transform.Rotation(WorldXvector, CurveTangentVector, SelectedPoint) 'rotation de vers Z
                            BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
                            MyBlocInstance.CommitChanges()
                            doc.Views.Redraw()
                        ElseIf OrientationLastValue = "Tangent" And OrientationOptions(OrientationSelectedIndex) = "Horizontal" Then
                            MyBlocInstance = doc.Objects.Find(BlocID)
                            MyTransform = Rhino.Geometry.Transform.Rotation(CurveTangentVector, Rhino.Geometry.Vector3d.Negate(WorldZvector), SelectedPoint) 'rotation de vers X
                            BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
                            MyBlocInstance.CommitChanges()
                            doc.Views.Redraw()
                        ElseIf OrientationLastValue = "Horizontal" And OrientationOptions(OrientationSelectedIndex) = "Tangent" Then
                            MyBlocInstance = doc.Objects.Find(BlocID)
                            MyTransform = Rhino.Geometry.Transform.Rotation(Rhino.Geometry.Vector3d.Negate(WorldZvector), CurveTangentVector, SelectedPoint) 'rotation de vers X
                            BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
                            MyBlocInstance.CommitChanges()
                            doc.Views.Redraw()
                        ElseIf OrientationLastValue = "Vertical" And OrientationOptions(OrientationSelectedIndex) = "Horizontal" Then
                            MyBlocInstance = doc.Objects.Find(BlocID)
                            MyTransform = Rhino.Geometry.Transform.Rotation(WorldXvector, Rhino.Geometry.Vector3d.Negate(WorldZvector), SelectedPoint) 'rotation de vers X
                            BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
                            MyBlocInstance.CommitChanges()
                            doc.Views.Redraw()
                        ElseIf OrientationLastValue = "Horizontal" And OrientationOptions(OrientationSelectedIndex) = "Vertical" Then
                            MyBlocInstance = doc.Objects.Find(BlocID)
                            MyTransform = Rhino.Geometry.Transform.Rotation(Rhino.Geometry.Vector3d.Negate(WorldZvector), WorldXvector, SelectedPoint) 'rotation de vers X
                            BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
                            MyBlocInstance.CommitChanges()
                            doc.Views.Redraw()
                        End If
                    End If
                    OrientationLastValue = OrientationOptions(OrientationSelectedIndex)
                End If

                'Inversion du sens en fonction de l'option
                If Inverser.CurrentValue <> InverserLastValue Then
                    If OrientationLastValue = "Tangent" Then
                        MyBlocInstance = doc.Objects.Find(BlocID)
                        MyTransform = Rhino.Geometry.Transform.Rotation(-Math.PI, plane.Normal, SelectedPoint) 'rotation de 90° suivant CurveTangentVector
                        BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
                        MyBlocInstance.CommitChanges()
                        doc.Views.Redraw()
                    ElseIf OrientationLastValue = "Vertical" Then
                        MyBlocInstance = doc.Objects.Find(BlocID)
                        MyTransform = Rhino.Geometry.Transform.Rotation(-Math.PI, WorldZvector, SelectedPoint) 'rotation de 90° suivant CurveTangentVector
                        BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
                        MyBlocInstance.CommitChanges()
                        doc.Views.Redraw()
                    ElseIf OrientationLastValue = "Horizontal" Then
                        MyBlocInstance = doc.Objects.Find(BlocID)
                        MyTransform = Rhino.Geometry.Transform.Rotation(-Math.PI, WorldXvector, SelectedPoint) 'rotation de 90° suivant CurveTangentVector
                        BlocID = doc.Objects.Transform(MyBlocInstance, MyTransform, True)
                        MyBlocInstance.CommitChanges()
                        doc.Views.Redraw()
                    End If

                End If
                InverserLastValue = Inverser.CurrentValue

                Continue While
            Else
                Exit While
            End If
        End While

        'Faire l'extrusion
        For Each SelectedObj In doc.Objects.GetSelectedObjects(True, True)
            SelectedObj.Select(False)
        Next
        MyBlocInstance = doc.Objects.Find(BlocID)
        MyBlocInstance.Select(True)
        Rhino.RhinoApp.RunScript("-_Explode ENTER", False)
        Rhino.RhinoApp.RunScript("-_ExtrudeCrv Bothsides=Yes 200 ENTER", False)
        Rhino.RhinoApp.RunScript("-_delete", False)
        Rhino.RhinoApp.RunScript("-_selLast DeselectOthersBeforeSelect=Yes ENTER", False)
        Dim MyExtrusion As Rhino.DocObjects.RhinoObject
        For Each SelectedObj In doc.Objects.GetSelectedObjects(False, False)
            MyExtrusion = SelectedObj
        Next
        doc.Views.Redraw()

        'Découper le cuto
        'For Each obj As DocObjects.RhinoObject In doc.Objects.GetSelectedObjects(True, True)
        '    Dim refObj As New DocObjects.ObjRef(obj.Id)
        'Next


        'Purger les blocs de cuto inutiles
        For Each item As Object In SelectionCutoBox.ListCuto.Items
            Try
                doc.InstanceDefinitions.Delete(doc.InstanceDefinitions.Find(item.ToString, True).Index, False, True)
            Catch ex As Exception
            End Try
        Next

        Return Result.Success
    End Function

Thanks for all this. But his is not going to help me repeat the problem you are having.

If you want help solving the BooleanDifference problem you are having, please provide a small, simple block of code that repeats the problem. If necessary, also provide any geometry that you are having a problem with.

Remember, the goal is to make it so we can easily repeat the problem here…