Scaling a Body in a Component

Scaling a Body in a Component

drews3Dprinteddesigns
Explorer Explorer
1,167 Views
5 Replies
Message 1 of 6

Scaling a Body in a Component

drews3Dprinteddesigns
Explorer
Explorer

Just wanted to preface this by saying I am very new to writing scripts and python, so my mistake could be a very obvious one. 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        
        # Create a document.
     #   doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
 
        design = adsk.fusion.Design.cast(app.activeProduct)

         # Get root component in this design
        rootComp = design.rootComponent
        
        # Create sketch
      #  sketches = rootComp.sketches
      #  sketch = sketches.add(rootComp.xZConstructionPlane)
      #  sketchCircles = sketch.sketchCurves.sketchCircles
      #  centerPoint = adsk.core.Point3D.create(0, 0, 0)
      #  circle = sketchCircles.addByCenterRadius(centerPoint, 5.0)
        
        # Get the profile defined by the circle
       # prof = sketch.profiles.item(0)

        # Create an extrusion input
       # extrudes = rootComp.features.extrudeFeatures
       # extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        
        # Define that the extent is a distance extent of 5 cm
       # distance = adsk.core.ValueInput.createByReal(5)
       # extInput.setDistanceExtent(False, distance)

        # Create the extrusion
       # ext = extrudes.add(extInput)
        
        # Get the first component
        occs = rootComp.occurrences
        subComp1 =occs.item(0).component
        
        # Get the first body in sub component 1  
        body = subComp1.bRepBodies.item(0)
        
        # Create a scale input
        inputColl = adsk.core.ObjectCollection.create()
        inputColl.add(body)
        
        basePt = adsk.core.Point3D.create(0, 0, 0)
        scaleFactor = adsk.core.ValueInput.createByReal(2)
        
        scales = rootComp.features.scaleFeatures
        scaleInput = scales.createInput(inputColl, basePt, scaleFactor)
        
        # Set the scale to be non-uniform
        xScale = adsk.core.ValueInput.createByReal(2)
        yScale = adsk.core.ValueInput.createByReal(1)
        zScale = adsk.core.ValueInput.createByReal(2)
        scaleInput.setToNonUniform(xScale, yScale, zScale)
        
        scale = scales.add(scaleInput)
        
        # Create another sketch
       # sketchVertical = sketches.add(rootComp.yZConstructionPlane)
       # sketchCirclesVertical = sketchVertical.sketchCurves.sketchCircles
       # centerPointVertical = adsk.core.Point3D.create(0, 10, 0)
       # cicleVertical = sketchCirclesVertical.addByCenterRadius(centerPointVertical, 5)
        
     #   # Create an uniformed input for scale feature input
     #   inputUniformColl = adsk.core.ObjectCollection.create()
       # inputUniformColl.add(sketchVertical)
        
      #  scaleUniformInput = scales.createInput(inputUniformColl, basePt, scaleFactor)
        
       # scaleUniform = scales.add(scaleUniformInput)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

I've by trying to scale up a body that is inside of a component, but every time I run this script (based off of this example) I receive the error below. 

 

What am I doing wrong? I used the "Get the first component" and "Get the first body in sub component 1" for the moveFeature, but this does not appear to work for the scaleFeature. 

0 Likes
1,168 Views
5 Replies
Replies (5)
Message 2 of 6

MichaelT_123
Advisor
Advisor

Hi Mr Drews3Dprinteddesigns,

 

pointGets and sets the origin point of the scale. This can be a BRepVertex, a SketchPoint or a ConstructionPoint

Consider correcting:

basePt = adsk.core.Point3D.create(0, 0, 0)

Regards

MichaelT

MichaelT
0 Likes
Message 3 of 6

drews3Dprinteddesigns
Explorer
Explorer

MichaelT, 

 

How would I correct that line with "point"?

 

The link for "point" appears to be broken.

 

Thanks, 

Drew

0 Likes
Message 4 of 6

MichaelT_123
Advisor
Advisor

Hi Mr Drews3Dprinteddesigns,

 

Just put your own personal effort to read documentation. I could tell you, but if you find the answer yourself you will remember it and have a greater satisfaction.

 

Regards

MichaelT

MichaelT
0 Likes
Message 5 of 6

drews3Dprinteddesigns
Explorer
Explorer

I managed to resolve the issue with the invalid reference point, but I can't figure out how to call on the first body in the first component. 

 

This is what I have so far:

 

       # Get root component in this design
       rootComp = design.rootComponent
        
       # Get the first sub component
       occs = rootComp.occurrences
       subComp1 = occs.item(0).component

       # Get the first body in sub component 1  
       bodyInSubComp1 = subComp1.bRepBodies.item(0)

 

 

 

        # Copy/paste body from sub component 1 to sub component 2
        copyPasteBody = subComp2.features.copyPasteBodies.add(bodyInSubComp1

This has previously worked in the Copy Feature (above), but doesn't work in the Scale Feature (below).

 

 

        # Create a scale input
       inputColl = adsk.core.ObjectCollection.create()
       inputColl.add(bodyInSubComp1)
        
       basePt = sketch.sketchPoints.item(0)
       scaleFactor = adsk.core.ValueInput.createByReal(2)
        
       scales = rootComp.features.scaleFeatures
       scaleInput = scales.createInput(inputColl, basePt, scaleFactor)
        
        # Set the scale to be non-uniform
       xScale = adsk.core.ValueInput.createByReal(1.5)
       yScale = adsk.core.ValueInput.createByReal(3)
       zScale = adsk.core.ValueInput.createByReal(2)
       scaleInput.setToNonUniform(xScale, yScale, zScale)
       
       scale = scales.add(scaleInput)

I've been reading the documentation, but can't seem to find what I'm looking for. I'm not looking for someone to fix my code, just point me in the right direction. 

 

Thanks, 

Drew

 

 

0 Likes
Message 6 of 6

MichaelT_123
Advisor
Advisor

Congrats Mr Drews3Dprinteddesigns,

 

... do not give up ... digg into docs ... and you will succeed!

 

Regards

MichaelT

 

MichaelT
0 Likes