Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Different height calculation for sweep and extrusion?

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
marcinszydlowski.1984
752 Views, 8 Replies

Different height calculation for sweep and extrusion?

Hello everyone,

 

my goal is to make a twisted sweep feature guided by path with known height (ex. 30.0 / 4.5) and later, merge this sweep with a cyllinder with the same height. It's important for get a smooth top surface (one planar BRepFace). All of this I can perform by API but I noticed that for some cases, especially for irrational numbers, I cannot get such surface because of difference in heights - left body:

 

Two bodies - left with with faultTwo bodies - left with with faultA - height differenceA - height difference

 

Here's my code used for the test:

import adsk.core, adsk.fusion, traceback
import math

def createPath(sketches: adsk.fusion.Sketches,
               plane: adsk.fusion.ConstructionPlane,
               coordinates: adsk.core.Point2D,
               height: float) -> adsk.fusion.Path:
    pathSketch = sketches.add(plane)
    pathSketch.name = "Path"
    pathSketch.isVisible = False
    line = pathSketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(coordinates.x, coordinates.y, 0), adsk.core.Point3D.create(coordinates.x, coordinates.y, height))
    path = plane.component.features.createPath(line)

    return path

def createSweep(profile: adsk.fusion.Profile,
                path: adsk.fusion.Path,
                guide: adsk.fusion.Path = None,
                twistAngle: float = 0) -> adsk.fusion.SweepFeature:
    sweeps = profile.parentSketch.parentComponent.features.sweepFeatures
    sweepInput = sweeps.createInput(profile, path, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

    if guide:
        sweepInput.guideRail = guide

    sweepInput.profileScaling = adsk.fusion.SweepProfileScalingOptions.SweepProfileNoScalingOption
    sweepInput.twistAngle = adsk.core.ValueInput.createByReal(twistAngle)
    sweep = sweeps.add(sweepInput)

    return sweep

def extrudeProfile(profile: adsk.fusion.Profile,
                   distance: float,
                   featureOperation: adsk.fusion.FeatureOperations = adsk.fusion.FeatureOperations.NewBodyFeatureOperation) -> adsk.fusion.ExtrudeFeature:

    distanceInput = adsk.core.ValueInput.createByReal(distance)
    extrusionResult = profile.parentSketch.parentComponent.features.extrudeFeatures.addSimple(profile, distanceInput, featureOperation)

    return extrusionResult

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        rootComp = design.rootComponent

        # 1. First test - thickness as a periodic irrational number - top faces no coincident
        # Prepare profile for twisting
        cuboidBaseCenter = adsk.core.Point3D.create(0, 2, 0)
        cuboidBaseCorner = adsk.core.Point3D.create(-0.5, 3, 0)
        sketches = rootComp.sketches
        twistedCuboidSketch = sketches.add(rootComp.xYConstructionPlane)
        twistedCuboidSketch.name = "Twisted cuboid sketch 1"
        twistedCuboidSketch.isVisible = False
        twistedCuboidSketch.sketchCurves.sketchLines.addCenterPointRectangle(cuboidBaseCenter, cuboidBaseCorner)

        # Prepare profile for core
        coreRadius = 1.3
        coreCenter = rootComp.originConstructionPoint.geometry
        coreSketch = sketches.add(rootComp.xYConstructionPlane)
        coreSketch.name = "Core sketch 1"
        coreSketch.isVisible = False
        coreSketch.sketchCurves.sketchCircles.addByCenterRadius(coreCenter, coreRadius)

        # Create twisted cuboid
        thickness = 30.0 / 4.5
        path = createPath(sketches, rootComp.xYConstructionPlane, adsk.core.Point2D.create(0, 0), thickness)
        createSweep(twistedCuboidSketch.profiles.item(0), path, twistAngle=math.pi / 2)

        # Merge it with core
        extrudeProfile(coreSketch.profiles.item(0), thickness, adsk.fusion.FeatureOperations.JoinFeatureOperation)

        # 2. Second test - thickness as a finite number - top faces coincident
        # Prepare profile for twisting
        cuboidBaseCenter = adsk.core.Point3D.create(10, 2, 0)
        cuboidBaseCorner = adsk.core.Point3D.create(9.5, 3, 0)
        sketches = rootComp.sketches
        twistedCuboidSketch = sketches.add(rootComp.xYConstructionPlane)
        twistedCuboidSketch.name = "Twisted cuboid sketch 2"
        twistedCuboidSketch.isVisible = False
        twistedCuboidSketch.sketchCurves.sketchLines.addCenterPointRectangle(cuboidBaseCenter, cuboidBaseCorner)

        # Prepare profile for core
        coreRadius = 1.3
        coreCenter = adsk.core.Point3D.create(10, 0, 0)
        coreSketch = sketches.add(rootComp.xYConstructionPlane)
        coreSketch.name = "Core sketch 2"
        coreSketch.isVisible = False
        coreSketch.sketchCurves.sketchCircles.addByCenterRadius(coreCenter, coreRadius)

        # Create twisted cuboid
        thickness = 30.0 / 1.5
        path = createPath(sketches, rootComp.xYConstructionPlane, adsk.core.Point2D.create(10, 0), thickness)
        createSweep(twistedCuboidSketch.profiles.item(0), path, twistAngle=math.pi / 2)

        # Merge it with core
        extrudeProfile(coreSketch.profiles.item(0), thickness, adsk.fusion.FeatureOperations.JoinFeatureOperation)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

I also tried:

  • extrude circle to top twisted body (as isMinSolution = False) but I got error because circle profile is outside of the top face,
  • make a plane on top face of twisted body (offset zero) and extrude the circle to this plane but it caused error which closed the Fusion,
  • get a point from the top face (face.pointOnFace) and extrude the circle to facePoint.z but it doesn't help - result the same.

Of course, if hight is a fraction I can prepare higher (height as int number) body with apropriate modified angle and later cut this body for desired size, but is there a better solution?

 

Thank you

8 REPLIES 8
Message 2 of 9

Hi marcinszydlowski.1984.

 

I thought about using a plane, but I tried using a formula.
Perhaps I thought that the data would be cleaner.

 

30.png31.png32.png33.png

 

Attach the sprict file.
It was also a study for me.

Message 3 of 9

Thank you very much, it works!

 

But if I understand correctly, the clue for making this working is setting the extrusion by expresion rather than distance input? Or there is no rule and I should always use such method for every features and operations when dimensions needed? 

Message 4 of 9

The reason is that "30.0 / 4.5" is an Irrational number (Circulating decimal).

 

Added to the first code.

・・・
        # Merge it with core
        extrude = extrudeProfile(coreSketch.profiles.item(0), thickness, adsk.fusion.FeatureOperations.JoinFeatureOperation)

        #path length
        pathLine = adsk.fusion.SketchLine.cast(path.item(0).entity) 
        geoSt = pathLine.startSketchPoint.geometry
        geoEd = pathLine.endSketchPoint.geometry
        pathLengthResult = geoSt.distanceTo(geoEd)
        
        #extrude distance
        extrudeDistanceResult = extrude.extentOne.distance.value

        ui.messageBox('pathLength:{}\nextrudeDistance:{}'
            .format(pathLengthResult,extrudeDistanceResult))


        # 2. Second test - thickness as a finite number - top faces coincident
・・・

The result is as follows.

40.png

 

Until using the API, it may be better to use mathematical formulas.

Message 5 of 9

I know that but I still don't understand what's the difference - in one operation number is not rounded on fourth decimal place but on the other it is. But ok, I'll be remember to be careful with extrusions.

 

Thank you

Message 6 of 9

I was interested, so I tried a little more.

 

Even without using an formula, just assigning the value to the value of modelParameter again gave the correct result.
Just modify the extrudeProfile function in the first code and you will get the correct result.

 

・・・

def extrudeProfile(profile: adsk.fusion.Profile,
                   distance: float,
                   featureOperation: adsk.fusion.FeatureOperations = adsk.fusion.FeatureOperations.NewBodyFeatureOperation) -> adsk.fusion.ExtrudeFeature:

    distanceInput = adsk.core.ValueInput.createByReal(distance)
    extrusionResult = profile.parentSketch.parentComponent.features.extrudeFeatures.addSimple(profile, distanceInput, featureOperation)

    #Enter an value in modelParam
    distParamName = extrusionResult.extentOne.distance.name
    modelParams =  extrusionResult.parentComponent.parentDesign.rootComponent.modelParameters
    param = modelParams.itemByName(distParamName)
    param.value = distance

    return extrusionResult

・・・

I thought "ValueInput.createByReal" might be the cause and created the following test code.

import adsk.core

def run(context):
    app = adsk.core.Application.get()
    ui  = app.userInterface

    float_Value = 30.0/4.5
    ValueInputReal = adsk.core.ValueInput.createByReal(float_Value)

    ui.messageBox('float_Value : {}\nValueInputReal : {}'
        .format(float_Value,ValueInputReal.realValue))

1.png

This is not the cause....

 

 

Message 7 of 9

I was also interested and finally found the cause. In case of every feature which need "valueInput" for entering dimensions/angles the value in such input is automatically rounded according to general precision (definded in "preferences" window):

 

Rounding error less than tolerance of mergingRounding error less than tolerance of mergingRounding error greater than tolerance of mergingRounding error greater than tolerance of merging

 

I think in the second case error is bigger than tollerance and merging bodies doesn't treat those two BRepFace as one.

Message 8 of 9

So the only thing is to remember to set greater precision before the operation and restore after it.

Message 9 of 9

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report