Color after boolean operation

Forum for reporting problems
Post Reply
Richard White
Posts: 0
Joined: Mon Aug 14, 2017 4:40 am

Color after boolean operation

Post by Richard White »

Good day,

I do a Bool Op subtract, which works, but I have an issue with the color of the result.

What I get is as follows: you can see that the teeth have different colors for the Pinion and Gear, but both hubs are of the same color, even if I impose the color when doing the Bool Op. And the color of the hubs has nothing to do with that of the teeth.

Here is my code. What is missing in there ?

Thanks, Richard

Code: Select all

   ' ---------------------------
   ' Create the Inner Cylinder
   ' ---------------------------
   '
   ISection2 = iStdShape2.Cylinder(Gen.RayIntHub, Gen.RayIntHub, RLong * 2)

   ' Give it color
   '
   Dim IMatl2   As IMaterial = DirectCast(iSection2, IMaterial)
   IMatl2.SetColor(1, CDec(IRed / 255), CDec(IGreen / 255), CDec(IBlue / 255))

   ' Transform location
   '
   Dim i3DObj2  As I3DObject  = DirectCast(iSection2, I3DObject)
   i3DObj2.Translate(0, 0, -RLong / 2#)
   i3DObj2.Rotate(-Gen.Angl2 * Gen.Ig / Degr, 0, 0, 0, 0, 1, 0)

   If Gen.IChoi = GeoType.Herringbone Then
      i3DObj2.Translate(0#, 0#, -Gen.L / 2#)
   End If

   i3DObj2.Translate(-Gen.Dist1, -Gen.HypoiD2, Gen.Dist3)

   ' ---------------------------
   ' Create the Outer Cylinder/Cone
   ' ---------------------------
   '
   iSection  = iStdShape.Cylinder(RayToe, RayHeel, RLong)

   ' Give it coulor
   '
   Dim IMatl   As IMaterial = DirectCast(iSection, IMaterial)
   IMatl.SetColor(1, CDec(IRed / 255), CDec(IGreen / 255), CDec(IBlue / 255))

   ' Transform location
   '
   Dim i3DObj  As I3DObject  = DirectCast(iSection, I3DObject)
   i3DObj.Rotate(-Gen.Angl2 * Gen.Ig / Degr, 0, 0, 0, 0, 1, 0)

   If Gen.IChoi = GeoType.Herringbone Then
      i3DObj.Translate(0#, 0#, -Gen.L / 2#)
   End If

   i3DObj.Translate(-Gen.Dist1, -Gen.HypoiD2, Gen.Dist3)

   ' ---------------------------
   ' Remove Inner Cylinder
   ' ---------------------------
   '
   iBoolSection = iSection
   iBoolSection.execute(EBooleanOperation.eBoolOpSubtract, ISection2, True)

   ' Give it color
   '
   Dim IMatl3   As IMaterial = DirectCast(iBoolSection, IMaterial)
   IMatl3.SetColor(1, CDec(IRed / 255), CDec(IGreen / 255), CDec(IBlue / 255))

   ' ---------------------------
   ' Update
   ' ---------------------------
   '
   ChildWindow(IActWin).AxKCad.UpdateSurface()
   ChildWindow(IActWin).AxKCad.UpdateView()

nickz
Site Admin
Posts: 236
Joined: Fri Jul 26, 2013 3:58 am

Re: Color after boolean operation

Post by nickz »

Hi Richard,
The operation creates a news section. You need to re-acquire it (and IMaterial) from the model (or parent section) the same way the initial (first) section was obtained. This happens only for non-MeshSection objects. We will do this a little better in v6.0.
The old section is invisible and will be destructed when the last reference goes out of scope

Richard White
Posts: 0
Joined: Mon Aug 14, 2017 4:40 am

Re: Color after boolean operation

Post by Richard White »

Hello Nick,

Thanks for being back to me.

I do re-acquire the Section and impose it the color, but it does not seem to reply. See my code below.

Code: Select all

         Dim iModel           As IModel         = ChildWindow(IDesWin).AxKCad.GetModel()
         Dim iKCArray         As IArray         = DirectCast(iModel,  IArray) 
         Dim NumSect          As Int32          = iModel.GetSectionCount() - 1
         Dim iPropertyArray   As IPropertyArray = DirectCast(iModel, IPropertyArray)
         Dim iStdShape        As IStdShape      = iPropertyArray.GetProperty("IKO_Model")

         Dim iSection         As ISection                      '= iModel.GetSection(NumSect)
         Dim ISection2        As ISection                      '= iModel.GetSection(NumSect)
         Dim iBoolSection     As IBoolSectionEx

         ' ---------------------------
         ' Hub Color
         ' ---------------------------
         '
         If Gen.Ig = -1 Then
            IColor = IColWinData(5, IDesWin)
         Else
            IColor = IColWinData(6, IDesWin)
         End If

         Call GetRgbFt(IColor, IRed, IGreen, IBlue)

         ' ---------------------------
         ' ---------------------------
         '

         Call GetNBotNFormNTip(0, Gen,  1#, _
                               NBot, NForm, NTip, Nx3, NStep)

         ' ---------------------------
         ' Radii
         ' ---------------------------
         '
         RayToe   = Math.Sqrt(Dent(1,   NBot, 1)^2 + _
                              Dent(1,   NBot, 2)^2)

         RayHeel  = Math.Sqrt(Dent(Nx3, NBot, 1)^2 + _
                              Dent(Nx3, NBot, 2)^2)

         RLong    = Dent(Nx3, NBot, 3) - Dent(1, NBot, 3)

         RInt     = Gen.RayIntHub

         DDist3   = -Gen.L / 2#

         ' ---------------------------
         ' Create the Inner Cylinder
         ' ---------------------------
         '
         ISection2 = iStdShape.Cylinder(RInt, RInt, RLong * 2)

         ' Give it color
         '
         Call SetKCiSectionColor(iSection2, IColor)

         ' Transformation
         '
         Call KCiSectionTransform(iSection2, Gen, AngRot, DDist3)

         ' ---------------------------
         ' Create the Outer Cylinder/Cone
         ' ---------------------------
         '
         iSection = iStdShape.Cylinder(RayToe, RayHeel, RLong)

         ' Give it color
         '
         Call SetKCiSectionColor(ISection, IColor)

         ' Transformation
         '
         Call KCiSectionTransform(iSection, Gen, AngRot, DDist3)

         ' ---------------------------
         ' Remove Inner Cylinder
         ' ---------------------------
         '
         iBoolSection = iSection
         iBoolSection.execute(EBooleanOperation.eBoolOpSubtract, ISection2, True)

         ' Give color to result
         '
         Call SetKCiSectionColor(iBoolSection, IColor)
         Call SetKCiSectionColor(iSection,     IColor)
         ChildWindow(IDesWin).AxKCad.UpdateSurface()
         ChildWindow(IDesWin).AxKCad.UpdateView()

And here is the Color sub:

Code: Select all

   Sub SetKCiSectionColor(ByRef iSection      As ISection, _
                          ByRef IColDes       As Int32) 

      Dim IRed, IGreen, IBlue As Int32
      Dim IMatl               As IMaterial =  iSection

      Call GetRgbFt(IColDes, IRed, IGreen, IBlue)

      IMatl.SetColor(1, CDeC(IRed / 255), CDec(IGreen / 255), CDec(IBlue / 255))
   End Sub


Alas, the result is the same: no color for the hub. Must be something else, or I must use another function ?
Any other idea / suggestion ?

Regards, Richard

nickz
Site Admin
Posts: 236
Joined: Fri Jul 26, 2013 3:58 am

Re: Color after boolean operation

Post by nickz »

Sorry, Richard,
I was wrong about getting it in the same way. The new section is added as the last object of the model
This test, which I tried in Object Array sample, is working. I get a blue object:

Code: Select all

   Sub ClaudesTest(iStdShape As IStdShape)
        Dim iSect As ISection = iStdShape.Cylinder(2, 2, 2)
        Dim iSect2 As ISection = iStdShape.Cylinder(1, 1, 3)
        Dim i3DObj2 As I3DObject = iSect2
        i3DObj2.Translate(0, 0, -0.5)

        Dim iBoolSection As IBoolSection = iSect
        iBoolSection.Subtract(iSect2)

        Dim iSectResult As ISection = m_iModel.GetSection(1)
        Dim iMater As IMaterial = iSectResult
        iMater.SetColor(0, 0, 0, 1)
        iMater.SetColor(1, 0, 0, 1)
    End Sub
In you code I do not see reacquiring ISection of the new object. You use IBoolSectionEx of the old object
You need something similar to the Dim iSectResult As ISection = m_iModel.GetSection(1) line above

Regards
Nick

Richard White
Posts: 0
Joined: Mon Aug 14, 2017 4:40 am

Re: Color after boolean operation

Post by Richard White »

Hi Nick
Thanks for pointing me in the right direction. It works great now
Richard

Post Reply