API to Untrim Surface

edit :  See here for example answer

 Untrim face or surface with temporary geometry using SOLIDWORKS API 

Hi Guys 

I'm looking for the best method to Untrim a face from my model.

Just to be clear I'm not talking about Inserting an Untrim Feature, but using temp bodies.

I had a look through IFace2 and ISurface but nothing jumped out at me.

I've managed to make a box of the correct size using GetUVBounds(), but then I'm not sure what to to with the rotation part of the transform as you can see below.

I think I have a way to do it, but it seems way more complicated than it should be.

Given an IFace2 object that's planar is there a straight forward way?

Thanks

SolidworksApi/macros
Answers 15
JB
JB 2021-04-08 Best Answer

Sorry, I'm a bit slow on the uptake this morning... What is it you're shooting for?  It looks like what's up there is an end-view of some kind of millwork/molding, and you're trying to create a rectangular surface body that corresponds to the raw size, in place?  

RE
RE 2021-04-08

That's so much nicer.

I did think of using iso curves, and was going to try them out, but it took far longer for me to get this working than I hoped.

I had an annoying typo bug in one of my loops, that took ages to spot.

I really like how you just keep it simple.

One of the reasons I chose to try creating my own lines is because that's part of next goal. - they're not scary anymore!

I'll add the link to your post to my question, so people can see it straight away.

I learnt a lot yesterday and already today. 

AT
AT 2021-04-08

Very good solution, Rob! You might also employ your idea with iso curves instead of explicitly building the boundary lines. In this case you will be able to untrim any surface (not just planar). If anyone needs here is VBA example: Untrim face or surface with temporary geometry using SOLIDWORKS API . By some reasons CreateTrimmedSheetX doesn't like B-spline curves and always fails for me, only works for planar and cylindrical faces with linear and arc curves. If anyone has any idea - would be nice if you can share. Basically just run the macro on any surface created from spline - it would fail, but will work OK for any planar surface or any cylindrical surface.

RE
RE 2021-04-08

Well, it actually took me longer to do than the other way.

I had a small bug I didn't spot for a quite a while.

I bet there's a much better way, but it does work.

Thanks yet again Artem

using SolidWorks.Interop.sldworks;
using System;

// namespace removed

public static class IModelerExtension
{
   public static IBody2 Untrim(this IModeler modeler, IFace2 face)
   {
      var uvBounds = face.GetUVBounds() as double[];

      const int uMin = 0, uMax = 1, vMin = 2, vMax = 3;

      var uvBox = new (double u, double v)[] { (uvBounds[uMin], uvBounds[vMin]),
                                               (uvBounds[uMin], uvBounds[vMax]),
                                               (uvBounds[uMax], uvBounds[vMax]),
                                               (uvBounds[uMax], uvBounds[vMin])};
      var surface = face.IGetSurface();

      double[][] points = new double[4][];

      for (int i = 0; i < 4; i++)
      {
         points[i] = surface.Evaluate(uvBox[i].u, uvBox[i].v, NumUDeriv: 0, NumVDeriv: 0) as double[];
      }

      var trimLines = new Curve[4];
      for (int i = 0; i < 4; i++)
      {
         int j = (i + 1) % 4;
         var line = (Curve)modeler.CreateLine(points[i], Direction(points[i], points[j]));

         trimLines[i] = line.CreateTrimmedCurve2(X1: points[i][0], Y1: points[i][1], Z1: points[i][2],
                                                 X2: points[j][0], Y2: points[j][1], Z2: points[j][2]);

         double[] Direction(double[] p1, double[] p2)
         {
            var vector = new double[] { p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2] };

            // Do I need to normalize?
            var mag = Math.Sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
            vector[0] /= mag; vector[1] /= mag; vector[2] /= mag;
            return vector;
         }
      }
      return surface.ICopy().CreateTrimmedSheet5(Curves: trimLines, PreserveAnalyticCurves: true, Tolerance: 0.00001) as IBody2;
   }

}

RE
RE 2021-04-08

Awesome Artem!

I knew there'd be a simpler way.  (Where were you 5 hours ago ;p?)

RE
RE 2021-04-08

Hi Josh

I negated the angle and it seems to have done the trick

Math[s] makes my head hurt so much I'm absolutely wiped out

comment imagecomment imagecomment imagecomment image
AT
AT 2021-04-08

You can try ISurface::Copy (this should restore an untrimmed surface), then get uv bounds of original face and create curves box from the corners and CreateTrimmedSheet. I believe this is similar to how original untrim works.

RJ
RJ 2021-04-08

Hi Rob,

If you could use Modeler::CreateBodyFromBox() to generate a Surface body then you do not need CreatePlanarSurface2() API call. You can directly use that surface body in the last step.

I knew that CreatePlanarSurface() is obsolete now but it still useful here. This is because we do not need the plane orientation.

Additionally, you can use the Face Edges to get the correct orientation. May be the linear edge with maximum length is a good starting point for you.

Hope it helps!!!

Rajat Jain

JB
JB 2021-04-08

 

 

 

Is your normal vector just backwards?

comment imagecomment image
RE
RE 2021-04-08

Hi Rajat

I have a feeling I haven't specified my problem accurately enough, or I misunderstand your answer.

It's calculating the untrimmed face bounds that I'm having difficulty with.

But your answer has been very useful, as I have discovered a few new things in the API.

Notably you can use CreateBodyFromBox to create a Surface directly, which will save me two method calls.

What else I found interesting is that  

System.object CreatePlanarSurface(    System.object VRootPoint,    System.object VNormal ) 

 

is marked as Obsolete and in 

System.object CreatePlanarSurface2(    System.object VRootPoint,    System.object VNormal,    System.object VRef )

 

We know have a VRef parameter

Array of 3 doubles (x,y,z) defining the x-axis on the planar surface with root point, VRootPoint; this vector must be perpendicular to VNormal

I would expect a PlanesParams2 property, but I found no way of getting this other than by something like this

I'm getting a lot closer..  It's the right angle (but the wrong way)

comment imagecomment imagecomment imagecomment image
RE
RE 2021-04-08

Thankyou Rajat

I'm trying to follow your instructions..  will report back

RJ
RJ 2021-04-08

Hello Rob,

I think there can be a simpler approach. Kindly try these steps: -

  • Create a planar surface temporary body using Modeler::CreatePlanarSurface() - call this TempBody1.
  • Create a finite box body (from the face bounds) using Modeler::CreateBodyFromBox() - call this TempBody2. 
  • Cut TempBody1 with TempBody2 (using Body2::Opertions2()) to get the finite surface body - Call the resultant body as TempBody3.
  • Create Surface body from the face using Modeler::CreateBodyFromFaces2() - Call this TempBody4. 
  • Cut TempBody3 with TempBody4 using Body2::Operations2()- This is your resultant body.

Hope this helps!!!

Rajat Jain

RE
RE 2021-04-08

After much hunting around in the API I think I can calculate the missing information using MakeIsoCurve Method (ISurface)  which returns a Curve.  I can then use LineParams Property (ICurve) to calculate the angle.

Well that's the present plan

RE
RE 2021-04-08

I'm beginning to understand why it's not working..

I am creating a transform from the PlaneParams of the Face Surface.

I was hoping that I could create a new planar surface at the origin using the uvBounds  and then apply an Inverse Transformation.

But the plane params are just a Root Point and Normal Axis, so there is no information regarding orientation on the plane, just the plane itself.

So categorically this approach cannot work.

I need a new approach.

I could possibly use a bounding box, but surely - there must be something obvious I'm missing?

comment imagecomment imagecomment imagecomment image
RE
RE 2021-04-08

Yep that's about right.

What I want is the equivalent to this

comment imagecomment image