BRep Body definition Sample

Description

Demonstrates creating BRep bodies by BRepBodyDefinition.

Code Samples

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

def validate(discription, result, baseline):
    if type(result) is str: 
        if result != baseline:
            raise Exception(discription + ' is different!\n'
                           + 'Result is ' + result + '\n'
                           + 'Baseline is ' + baseline + '\n'
                           )
    elif type(result) is float: 
        if abs(result - baseline) > 10e-6:
            raise Exception(discription + ' is different!\n'
                           + 'Result is ' + str(result) + '\n'
                           + 'Baseline is ' + str(baseline) + '\n'
                           )
    elif type(result) is int: 
        if abs(result - baseline) > 0:
            raise Exception(discription + ' is different!\n'
                           + 'Result is ' + str(result) + '\n'
                           + 'Baseline is ' + str(baseline) + '\n'
                           )
    elif type(result) is bool: 
        if result != baseline:
            raise Exception(discription + ' is different!\n'
                           + 'Result is ' + str(result) + '\n'
                           + 'Baseline is ' + str(baseline) + '\n'
                           )
    else:
        raise Exception('Validaion is not support the data type ' + discription)
#endTest
        
def CreateTop1EdgeDefinitionByCurve(brepBodyDef):
    edgeDef = adsk.fusion.BRepEdgeDefinition.cast(None)
    # Create vertex definition
    startVertexDef = brepBodyDef.createVertexDefinition(adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957))
    endVertexDef = brepBodyDef.createVertexDefinition(adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.282227138902387)) 
    
    center = adsk.core.Point3D.create(-2.7379066676103037, 4.0, -0.3666720191852457)
    normal =adsk.core.Vector3D.create(0, 1, 0)
    curve = adsk.core.Circle3D.createByCenter(center, normal, 2.0689)
    # Create edge definition by curve
    edgeDef = brepBodyDef.createEdgeDefinitionByCurve(startVertexDef, endVertexDef, curve)
    
    return edgeDef    

def CreateBottom1EdgeDefinitionByCurve(brepBodyDef):
    edgeDef = adsk.fusion.BRepEdgeDefinition.cast(None)
    # Create vertex definition
    startVertexDef = brepBodyDef.createVertexDefinition(adsk.core.Point3D.create(-3.519568866597467, 0.0, -2.2822271389023863))
    endVertexDef = brepBodyDef.createVertexDefinition(adsk.core.Point3D.create(-3.519568866597467, 0.0, 1.5488831005318966)) 
    
    center = adsk.core.Point3D.create(-2.7379066676103037, 0.0, -0.36667201918524484)
    normal =adsk.core.Vector3D.create(0, -1, 0)
    curve = adsk.core.Circle3D.createByCenter(center, normal, 2.0689)
    # Create edge definition by curve
    edgeDef = brepBodyDef.createEdgeDefinitionByCurve(startVertexDef, endVertexDef, curve)
    
    return edgeDef

def CreateLinearEdgeDefinitionByCurve(brepBodyDef, startPoint, endPoint):
    edgeDef = adsk.fusion.BRepEdgeDefinition.cast(None)
    # Create vertex definition
    startVertexDef = brepBodyDef.createVertexDefinition(startPoint)
    endVertexDef = brepBodyDef.createVertexDefinition(endPoint)  
    curve = adsk.core.Line3D.create(startPoint, endPoint)
    # Create edge definition by curve
    edgeDef = brepBodyDef.createEdgeDefinitionByCurve(startVertexDef, endVertexDef, curve)
    
    return edgeDef   

def GetNurbsSurfaceMiddle1():
    surface = adsk.core.NurbsSurface.cast(None)
    
    degreeU = 1
    degreeV = 2
    controlPointCountU = 2
    controlPointcountV = 7
    controlPoints = []
    controlPoints.append(adsk.core.Point3D.create(-3.519568866597467, 0.0, 1.5488831005318966))
    controlPoints.append(adsk.core.Point3D.create(-2.0550441756714655, 0.0, 2.1464976398353937))
    controlPoints.append(adsk.core.Point3D.create(-1.0943307562984703, 0.0, 0.8899128103250742))
    controlPoints.append(adsk.core.Point3D.create(-0.13361733692547517, 0.0, -0.36667201918524484))
    controlPoints.append(adsk.core.Point3D.create(-1.0943307562984705, 0.0, -1.6232568486955643))
    controlPoints.append(adsk.core.Point3D.create(-2.0550441756714664, 0.0, -2.8798416782058833))
    controlPoints.append(adsk.core.Point3D.create(-3.5195688665974676, 0.0, -2.2822271389023863))
    controlPoints.append(adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957))
    controlPoints.append(adsk.core.Point3D.create(-2.0550441756714655, 4.0, 2.1464976398353928))
    controlPoints.append(adsk.core.Point3D.create(-1.0943307562984703, 4.0, 0.8899128103250733))
    controlPoints.append(adsk.core.Point3D.create(-0.13361733692547517, 4.0, -0.3666720191852457))
    controlPoints.append(adsk.core.Point3D.create(-1.0943307562984705, 4.0, -1.6232568486955652))
    controlPoints.append(adsk.core.Point3D.create(-2.0550441756714664, 4.0, -2.879841678205884))
    controlPoints.append(adsk.core.Point3D.create(-3.5195688665974676, 4.0, -2.282227138902387))
    knotsU = (-0.9666972787471603, -0.9666972787471603, 0.9666972787471603, 0.9666972787471603)
    knotsV = (-1.9582319586428225, -1.9582319586428225, -1.9582319586428225, -0.6527439862142743, -0.6527439862142743, 0.6527439862142739, 0.6527439862142739, 1.9582319586428225, 1.9582319586428225, 1.9582319586428225)
    weights = (1.0, 0.7944201804397665, 1.0, 0.7944201804397665, 1.0, 0.7944201804397665, 1.0, 1.0, 0.7944201804397665, 1.0, 0.7944201804397665, 1.0, 0.7944201804397665, 1.0)
    propertyU = 9
    propertyV = 9
    surface = adsk.core.NurbsSurface.create(degreeU, degreeV, controlPointCountU, controlPointcountV, controlPoints, knotsU, knotsV, weights, propertyU, propertyV)

    return surface
    
def GetNurbsSurfaceMiddle2():
    surface = adsk.core.NurbsSurface.cast(None)
    
    degreeU = 1
    degreeV = 1
    controlPointCountU = 2
    controlPointcountV = 2
    controlPoints = []
    controlPoints.append(adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.282227138902387))
    controlPoints.append(adsk.core.Point3D.create(-3.519568866597467, 0, -2.2822271389023863))
    controlPoints.append(adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957))
    controlPoints.append(adsk.core.Point3D.create(-3.519568866597467, 0, 1.5488831005318966))
    knotsU = (0, 0, 3.831110239434283, 3.831110239434283)
    knotsV = (0, 0, 4.0, 4.0)
    weights = ()
    propertyU = 1
    propertyV = 1
    surface = adsk.core.NurbsSurface.create(degreeU, degreeV, controlPointCountU, controlPointcountV, controlPoints, knotsU, knotsV, weights, propertyU, propertyV)
    
    return surface
    
def GetNurbsSurfaceTop():
    surface = adsk.core.NurbsSurface.cast(None)
    
    degreeU = 1
    degreeV = 1
    controlPointCountU = 2
    controlPointcountV = 2
    controlPoints = []
    controlPoints.append(adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.7441010118322937))
    controlPoints.append(adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.4732150714952117))
    controlPoints.append(adsk.core.Point3D.create(-0.6323626099308138, 4.0, 1.7441010118322937))
    controlPoints.append(adsk.core.Point3D.create(-0.6323626099308138, 4.0, -2.4732150714952117))
    knotsU = (-0.7816621989871636, -0.7816621989871636, 2.1055440576794897, 2.1055440576794897)
    knotsV = (-2.110773031017539, -2.110773031017539, 2.1065430523099655, 2.1065430523099655)
    weights = ()
    propertyU = 1
    propertyV = 1
    surface = adsk.core.NurbsSurface.create(degreeU, degreeV, controlPointCountU, controlPointcountV, controlPoints, knotsU, knotsV, weights, propertyU, propertyV)
    
    return surface
    
def GetNurbsSurfaceBottom():
    surface = adsk.core.NurbsSurface.cast(None)
    
    degreeU = 1
    degreeV = 1
    controlPointCountU = 2
    controlPointcountV = 2
    controlPoints = []
    controlPoints.append(adsk.core.Point3D.create(-0.6323626099308726, 0.0, 1.7398710331248253))
    controlPoints.append(adsk.core.Point3D.create(-0.6323626099308726, 0.0, -2.4774450502028094))
    controlPoints.append(adsk.core.Point3D.create(-3.5195688665974676, 0.0, 1.739871033124825))
    controlPoints.append(adsk.core.Point3D.create(-3.5195688665974676, 0.0, -2.47744505020281))
    knotsU = (-2.105544057679431, -2.105544057679431, 0.7816621989871635, 0.7816621989871635)
    knotsV = (-2.10654305231007, -2.10654305231007, 2.110773031017565, 2.110773031017565)
    weights = ()
    propertyU = 1
    propertyV = 1
    surface = adsk.core.NurbsSurface.create(degreeU, degreeV, controlPointCountU, controlPointcountV, controlPoints, knotsU, knotsV, weights, propertyU, propertyV)    
    
    return surface
    
def CreateFaceForBodyDefination(brepBodyDef, surface, edgeDefs):   
    faceDef = adsk.fusion.BRepFaceDefinition.cast(None)
    
    # Create lump definition
    lumpDefs = brepBodyDef.lumpDefinitions
    lumpDef = lumpDefs.add()
    # Create shell definition
    shellDefs = lumpDef.shellDefinitions
    shellDef = shellDefs.add()
    # Create face definition
    faceDefs = shellDef.faceDefinitions
    faceDef = faceDefs.add(surface, True)

    # Create loop definition
    loopDefs = faceDef.loopDefinitions
    loopdef = loopDefs.add()
    
    # Create coEdge definitions
    brepCoEdgeDefs = loopdef.bRepCoEdgeDefinitions   
    for edgeDef in edgeDefs:
        brepCoEdgeDefs.add(edgeDef, True)

    return faceDef   
    
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        # Create a new document
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        
        # Create a base feature
        rootComp = design.rootComponent
        
        # Create brep body definition     
        brepBodyDef = adsk.fusion.BRepBodyDefinition.create()
        
        # Create edge definition
        top1EdgeDef = CreateTop1EdgeDefinitionByCurve(brepBodyDef)
        bottom1EdgeDef = CreateBottom1EdgeDefinitionByCurve(brepBodyDef)
        
        startPoint = adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.282227138902387)
        endPoint = adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957)
        top2EdgeDef = CreateLinearEdgeDefinitionByCurve(brepBodyDef, startPoint, endPoint)
        
        startPoint = adsk.core.Point3D.create(-3.519568866597467, 0.0, 1.5488831005318966)
        endPoint = adsk.core.Point3D.create(-3.519568866597467, 0.0, -2.2822271389023863)
        bottom2EdgeDef = CreateLinearEdgeDefinitionByCurve(brepBodyDef, startPoint, endPoint)
        
        startPoint = adsk.core.Point3D.create(-3.519568866597467, 0.0, -2.2822271389023863)
        endPoint = adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.282227138902387)
        middle1EdgeDef = CreateLinearEdgeDefinitionByCurve(brepBodyDef, startPoint, endPoint)
        
        startPoint = adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957)
        endPoint = adsk.core.Point3D.create(-3.519568866597467, 0.0, 1.5488831005318966)
        middle2EdgeDef = CreateLinearEdgeDefinitionByCurve(brepBodyDef, startPoint, endPoint)

        # Create the top face for body definition
        topSurface = GetNurbsSurfaceTop() 
        topEdgeDefs = []
        topEdgeDefs.append(top1EdgeDef)
        topEdgeDefs.append(top2EdgeDef)
        CreateFaceForBodyDefination(brepBodyDef, topSurface, topEdgeDefs)
        # Create one middle face for body definition
        middel1Surface = GetNurbsSurfaceMiddle1()
        middel1EdgeDefs = []
        middel1EdgeDefs.append(top1EdgeDef)
        middel1EdgeDefs.append(middle1EdgeDef)
        middel1EdgeDefs.append(bottom1EdgeDef)
        middel1EdgeDefs.append(middle2EdgeDef)
        CreateFaceForBodyDefination(brepBodyDef, middel1Surface, middel1EdgeDefs)
        # Create another middle face for body defination        
        middel2Surface = GetNurbsSurfaceMiddle2()
        middel2EdgeDefs = []
        middel2EdgeDefs.append(top2EdgeDef)
        middel2EdgeDefs.append(middle1EdgeDef)
        middel2EdgeDefs.append(bottom2EdgeDef)
        middel2EdgeDefs.append(middle2EdgeDef)         
        CreateFaceForBodyDefination(brepBodyDef, middel2Surface, middel2EdgeDefs)
        # Create the bottom face for body definition        
        bottomSurface = GetNurbsSurfaceBottom()
        bottomEdgeDefs = []
        bottomEdgeDefs.append(bottom1EdgeDef)
        bottomEdgeDefs.append(bottom2EdgeDef)
        CreateFaceForBodyDefination(brepBodyDef, bottomSurface, bottomEdgeDefs)

        # Create brep body      
        body = brepBodyDef.createBody() 
        outcomeInfo = brepBodyDef.outcomeInfo
        print('The outcome info is :\n{}'.format(outcomeInfo))
        
        # Create wire brep body definition
        wireBrepBodyDef = adsk.fusion.BRepBodyDefinition.create() 
        # Create lump definition
        lumpDefs = wireBrepBodyDef.lumpDefinitions
        lumpDef = lumpDefs.add()
        # Create shell definition
        shellDefs = lumpDef.shellDefinitions
        shellDef = shellDefs.add()
        # Create wire edge definitions        
        wireDef = shellDef.wireDefinition
        wireEdgeDefs = wireDef.wireEdgeDefinitions
        
        # Add wire edge definition
        startPoint = adsk.core.Point3D.create(2.499830308814756, 4.786795576310287, 0.0)
        endPoint = adsk.core.Point3D.create(6.000163479757444, 2.0712231354540322, 0.0)
        line = adsk.core.Line3D.create(startPoint, endPoint)
                
        center = adsk.core.Point3D.create(3.3848741924869366, 2.313878842520367, 0.0)
        normal =adsk.core.Vector3D.create(0, 0, 1.0)
        circle = adsk.core.Circle3D.createByCenter(center, normal, 2.626)

        startVertexDef = wireBrepBodyDef.createVertexDefinition(startPoint)
        endVertexDef = wireBrepBodyDef.createVertexDefinition(endPoint) 
        lineWireEdgeDef = wireEdgeDefs.add(startVertexDef, endVertexDef, line)
        circleWireEdgeDef = wireEdgeDefs.add(startVertexDef, endVertexDef, circle)
        
        # Set wire edge associative id
        lineWireEdgeDef.associativeID = 10
        print('The linear wire edge defination associative id is: {}'.format(lineWireEdgeDef.associativeID))
              
        # Create wire brep body 
        wireBrepBodyDef.doFullHealing = False
        wireBrepBody = wireBrepBodyDef.createBody()
        print('The wire brep body defination do full healing is: {}'.format(wireBrepBodyDef.doFullHealing))
        
        # Show the temporary brep bodies
        group = rootComp.customGraphicsGroups.add()
        group.addBRepBody(body)
        
        for edge in wireBrepBody.edges:
            group.addCurve(edge.geometry)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
/**
 * BRep Body definition Sample
 * Demonstrates creating BRep bodies by BRepBodyDefinition.
 */

import { adsk } from '@adsk/fusion';

function validate(description: string, result: any, baseline: any): void {
  if (typeof result === 'string') {
    if (result !== baseline) {
      throw new Error(`${description} is different!\nResult is ${result}\nBaseline is ${baseline}\n`);
    }
  } else if (typeof result === 'number') {
    if (Math.abs(result - baseline) > 1e-6) {
      throw new Error(`${description} is different!\nResult is ${result}\nBaseline is ${baseline}\n`);
    }
  } else if (typeof result === 'boolean') {
    if (result !== baseline) {
      throw new Error(`${description} is different!\nResult is ${result}\nBaseline is ${baseline}\n`);
    }
  } else {
    throw new Error(`Validation does not support the data type ${description}`);
  }
}

function CreateTop1EdgeDefinitionByCurve(brepBodyDef: adsk.fusion.BRepBodyDefinition): adsk.fusion.BRepEdgeDefinition | null {
  // Create vertex definition
  const startVertexDef = brepBodyDef.createVertexDefinition(adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957))
  const endVertexDef = brepBodyDef.createVertexDefinition(adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.282227138902387))

  const center = adsk.core.Point3D.create(-2.7379066676103037, 4.0, -0.3666720191852457)
  const normal = adsk.core.Vector3D.create(0, 1, 0)
  const curve = adsk.core.Circle3D.createByCenter(center, normal, 2.0689)
  // Create edge definition by curve
  const edgeDef = brepBodyDef.createEdgeDefinitionByCurve(startVertexDef, endVertexDef, curve)

  return edgeDef
}

function CreateBottom1EdgeDefinitionByCurve(brepBodyDef: adsk.fusion.BRepBodyDefinition): adsk.fusion.BRepEdgeDefinition | null {
  // Create vertex definition
  const startVertexDef = brepBodyDef.createVertexDefinition(adsk.core.Point3D.create(-3.519568866597467, 0.0, -2.2822271389023863))
  const endVertexDef = brepBodyDef.createVertexDefinition(adsk.core.Point3D.create(-3.519568866597467, 0.0, 1.5488831005318966))

  const center = adsk.core.Point3D.create(-2.7379066676103037, 0.0, -0.36667201918524484)
  const normal = adsk.core.Vector3D.create(0, -1, 0)
  const curve = adsk.core.Circle3D.createByCenter(center, normal, 2.0689)
  // Create edge definition by curve
  const edgeDef = brepBodyDef.createEdgeDefinitionByCurve(startVertexDef, endVertexDef, curve)

  return edgeDef
}

function CreateLinearEdgeDefinitionByCurve(brepBodyDef: adsk.fusion.BRepBodyDefinition, startPoint: adsk.core.Point3D, endPoint: adsk.core.Point3D): adsk.fusion.BRepEdgeDefinition | null {
  // Create vertex definition
  const startVertexDef = brepBodyDef.createVertexDefinition(startPoint)
  const endVertexDef = brepBodyDef.createVertexDefinition(endPoint)
  const curve = adsk.core.Line3D.create(startPoint, endPoint)
  // Create edge definition by curve
  const edgeDef = brepBodyDef.createEdgeDefinitionByCurve(startVertexDef, endVertexDef, curve)

  return edgeDef
}

function GetNurbsSurfaceMiddle1(): adsk.core.NurbsSurface | null {
  const degreeU = 1;
  const degreeV = 2;
  const controlPointCountU = 2;
  const controlPointCountV = 7;
  const controlPoints = [
    adsk.core.Point3D.create(-3.519568866597467, 0.0, 1.5488831005318966),
    adsk.core.Point3D.create(-2.0550441756714655, 0.0, 2.1464976398353937),
    adsk.core.Point3D.create(-1.0943307562984703, 0.0, 0.8899128103250742),
    adsk.core.Point3D.create(-0.13361733692547517, 0.0, -0.36667201918524484),
    adsk.core.Point3D.create(-1.0943307562984705, 0.0, -1.6232568486955643),
    adsk.core.Point3D.create(-2.0550441756714664, 0.0, -2.8798416782058833),
    adsk.core.Point3D.create(-3.5195688665974676, 0.0, -2.2822271389023863),
    adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957),
    adsk.core.Point3D.create(-2.0550441756714655, 4.0, 2.1464976398353928),
    adsk.core.Point3D.create(-1.0943307562984703, 4.0, 0.8899128103250733),
    adsk.core.Point3D.create(-0.13361733692547517, 4.0, -0.3666720191852457),
    adsk.core.Point3D.create(-1.0943307562984705, 4.0, -1.6232568486955652),
    adsk.core.Point3D.create(-2.0550441756714664, 4.0, -2.879841678205884),
    adsk.core.Point3D.create(-3.5195688665974676, 4.0, -2.282227138902387)
  ];
  const knotsU = [-0.9666972787471603, -0.9666972787471603, 0.9666972787471603, 0.9666972787471603];
  const knotsV = [-1.9582319586428225, -1.9582319586428225, -1.9582319586428225, -0.6527439862142743, -0.6527439862142743, 0.6527439862142739, 0.6527439862142739, 1.9582319586428225, 1.9582319586428225, 1.9582319586428225];
  const weights = [1.0, 0.7944201804397665, 1.0, 0.7944201804397665, 1.0, 0.7944201804397665, 1.0, 1.0, 0.7944201804397665, 1.0, 0.7944201804397665, 1.0, 0.7944201804397665, 1.0];
  const propertyU = 9;
  const propertyV = 9;

  const surface = adsk.core.NurbsSurface.create(degreeU, degreeV, controlPointCountU, controlPointCountV, controlPoints, knotsU, knotsV, weights, propertyU, propertyV);

  return surface;
}

function GetNurbsSurfaceMiddle2(): adsk.core.NurbsSurface | null {
  const degreeU = 1;
  const degreeV = 1;
  const controlPointCountU = 2;
  const controlPointCountV = 2;
  const controlPoints = [
    adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.282227138902387),
    adsk.core.Point3D.create(-3.519568866597467, 0, -2.2822271389023863),
    adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957),
    adsk.core.Point3D.create(-3.519568866597467, 0, 1.5488831005318966)
  ];
  const knotsU = [0, 0, 3.831110239434283, 3.831110239434283];
  const knotsV = [0, 0, 4.0, 4.0];
  const weights: number[] = [];
  const propertyU = 1;
  const propertyV = 1;

  const surface = adsk.core.NurbsSurface.create(degreeU, degreeV, controlPointCountU, controlPointCountV, controlPoints, knotsU, knotsV, weights, propertyU, propertyV);

  return surface;
}

function GetNurbsSurfaceTop(): adsk.core.NurbsSurface | null {
  const degreeU = 1;
  const degreeV = 1;
  const controlPointCountU = 2;
  const controlPointCountV = 2;
  const controlPoints = [
    adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.7441010118322937),
    adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.4732150714952117),
    adsk.core.Point3D.create(-0.6323626099308138, 4.0, 1.7441010118322937),
    adsk.core.Point3D.create(-0.6323626099308138, 4.0, -2.4732150714952117)
  ];
  const knotsU = [-0.7816621989871636, -0.7816621989871636, 2.1055440576794897, 2.1055440576794897];
  const knotsV = [-2.110773031017539, -2.110773031017539, 2.1065430523099655, 2.1065430523099655];
  const weights: number[] = [];
  const propertyU = 1;
  const propertyV = 1;

  const surface = adsk.core.NurbsSurface.create(degreeU, degreeV, controlPointCountU, controlPointCountV, controlPoints, knotsU, knotsV, weights, propertyU, propertyV);

  return surface;
}

function GetNurbsSurfaceBottom(): adsk.core.NurbsSurface | null {
  const degreeU = 1;
  const degreeV = 1;
  const controlPointCountU = 2;
  const controlPointCountV = 2;
  const controlPoints = [
    adsk.core.Point3D.create(-0.6323626099308726, 0.0, 1.7398710331248253),
    adsk.core.Point3D.create(-0.6323626099308726, 0.0, -2.4774450502028094),
    adsk.core.Point3D.create(-3.5195688665974676, 0.0, 1.739871033124825),
    adsk.core.Point3D.create(-3.5195688665974676, 0.0, -2.47744505020281)
  ];
  const knotsU = [-2.105544057679431, -2.105544057679431, 0.7816621989871635, 0.7816621989871635];
  const knotsV = [-2.10654305231007, -2.10654305231007, 2.110773031017565, 2.110773031017565];
  const weights: number[] = [];
  const propertyU = 1;
  const propertyV = 1;

  const surface = adsk.core.NurbsSurface.create(degreeU, degreeV, controlPointCountU, controlPointCountV, controlPoints, knotsU, knotsV, weights, propertyU, propertyV);

  return surface;
}

function CreateFaceForBodyDefinition(brepBodyDef: adsk.fusion.BRepBodyDefinition, surface: adsk.core.NurbsSurface, edgeDefs: adsk.fusion.BRepEdgeDefinition[]): adsk.fusion.BRepFaceDefinition | null {

  // Create lump definition
  const lumpDefs = brepBodyDef.lumpDefinitions;
  const lumpDef = lumpDefs.add();

  // Create shell definition
  const shellDefs = lumpDef.shellDefinitions;
  const shellDef = shellDefs.add();

  // Create face definition
  const faceDefs = shellDef.faceDefinitions;
  const faceDef = faceDefs.add(surface, true);

  // Create loop definition
  const loopDefs = faceDef.loopDefinitions;
  const loopDef = loopDefs.add();

  // Create coEdge definitions
  const brepCoEdgeDefs = loopDef.bRepCoEdgeDefinitions;
  for (const edgeDef of edgeDefs) {
    brepCoEdgeDefs.add(edgeDef, true);
  }

  return faceDef;
}


function run() {

  // Get the Fusion API's application object
  const app = adsk.core.Application.get();
  if (!app) throw Error("No adsk.core.Application.");

  // Create a document.
  const doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

  // Create brep body definition
  const brepBodyDef = adsk.fusion.BRepBodyDefinition.create();

  // Create edge definition
  const top1EdgeDef = CreateTop1EdgeDefinitionByCurve(brepBodyDef);
  const bottom1EdgeDef = CreateBottom1EdgeDefinitionByCurve(brepBodyDef);

  let startPoint = adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.282227138902387);
  let endPoint = adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957);
  const top2EdgeDef = CreateLinearEdgeDefinitionByCurve(brepBodyDef, startPoint, endPoint);

  startPoint = adsk.core.Point3D.create(-3.519568866597467, 0.0, 1.5488831005318966);
  endPoint = adsk.core.Point3D.create(-3.519568866597467, 0.0, -2.2822271389023863);
  const bottom2EdgeDef = CreateLinearEdgeDefinitionByCurve(brepBodyDef, startPoint, endPoint);

  startPoint = adsk.core.Point3D.create(-3.519568866597467, 0.0, -2.2822271389023863);
  endPoint = adsk.core.Point3D.create(-3.519568866597467, 4.0, -2.282227138902387);
  const middle1EdgeDef = CreateLinearEdgeDefinitionByCurve(brepBodyDef, startPoint, endPoint);

  startPoint = adsk.core.Point3D.create(-3.519568866597467, 4.0, 1.5488831005318957);
  endPoint = adsk.core.Point3D.create(-3.519568866597467, 0.0, 1.5488831005318966);
  const middle2EdgeDef = CreateLinearEdgeDefinitionByCurve(brepBodyDef, startPoint, endPoint);

  // Create the top face for body definition
  const topSurface = GetNurbsSurfaceTop();
  const topEdgeDefs = [top1EdgeDef, top2EdgeDef];
  CreateFaceForBodyDefinition(brepBodyDef, topSurface, topEdgeDefs);

  // Create one middle face for body definition
  const middle1Surface = GetNurbsSurfaceMiddle1();
  const middle1EdgeDefs = [top1EdgeDef, middle1EdgeDef, bottom1EdgeDef, middle2EdgeDef];
  CreateFaceForBodyDefinition(brepBodyDef, middle1Surface, middle1EdgeDefs);

  // Create another middle face for body definition
  const middle2Surface = GetNurbsSurfaceMiddle2();
  const middle2EdgeDefs = [top2EdgeDef, middle1EdgeDef, bottom2EdgeDef, middle2EdgeDef];
  CreateFaceForBodyDefinition(brepBodyDef, middle2Surface, middle2EdgeDefs);

  // Create the bottom face for body definition
  const bottomSurface = GetNurbsSurfaceBottom();
  const bottomEdgeDefs = [bottom1EdgeDef, bottom2EdgeDef];
  CreateFaceForBodyDefinition(brepBodyDef, bottomSurface, bottomEdgeDefs);

  // Create brep body
  const body = brepBodyDef.createBody();
  const outcomeInfo = brepBodyDef.outcomeInfo;
  adsk.log(`The outcome info is: ${outcomeInfo}`);

  // Create wire brep body definition
  const wireBrepBodyDef = adsk.fusion.BRepBodyDefinition.create();

  // Create lump definition
  const lumpDefs = wireBrepBodyDef.lumpDefinitions;
  const lumpDef = lumpDefs.add();

  // Create shell definition
  const shellDefs = lumpDef.shellDefinitions;
  const shellDef = shellDefs.add();

  // Create wire edge definitions
  const wireDef = shellDef.wireDefinition;
  const wireEdgeDefs = wireDef.wireEdgeDefinitions;

  // Add wire edge definition
  startPoint = adsk.core.Point3D.create(2.499830308814756, 4.786795576310287, 0.0);
  endPoint = adsk.core.Point3D.create(6.000163479757444, 2.0712231354540322, 0.0);
  const line = adsk.core.Line3D.create(startPoint, endPoint);

  const center = adsk.core.Point3D.create(3.3848741924869366, 2.313878842520367, 0.0);
  const normal = adsk.core.Vector3D.create(0, 0, 1.0);
  const circle = adsk.core.Circle3D.createByCenter(center, normal, 2.626);

  const startVertexDef = wireBrepBodyDef.createVertexDefinition(startPoint);
  const endVertexDef = wireBrepBodyDef.createVertexDefinition(endPoint);
  const lineWireEdgeDef = wireEdgeDefs.add(startVertexDef, endVertexDef, line);
  const circleWireEdgeDef = wireEdgeDefs.add(startVertexDef, endVertexDef, circle);

  // Set wire edge associative id
  lineWireEdgeDef.associativeID = 10;
  adsk.log(`The linear wire edge definition associative id is: ${lineWireEdgeDef.associativeID}`);

  // Create wire brep body
  wireBrepBodyDef.doFullHealing = false;
  const wireBrepBody = wireBrepBodyDef.createBody();
  adsk.log(`The wire brep body definition do full healing is: ${wireBrepBodyDef.doFullHealing}`);

}
run();