这是我的py代码,用于在平板上建模随机孔。为什么此代码报告以下错误:(IndexError: Sequence index out of range)?我该如何修理?

-编码:mbcs-

from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *
import random
a=100
b=50
t=8

mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)
mdb.models['Model-1'].sketches['__profile__'].rectangle(point1=(0.0, 0.0),
    point2=(a, b))
mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=
    DEFORMABLE_BODY)
i=0
while i<10:
    r=10.0
    x0=random.random(20.0,80.0)
    y0=50*random.random(10.0,40.0)
    mdb.models['Model-1'].parts['Part-1'].BaseSolidExtrude(depth=t, sketch=
    mdb.models['Model-1'].sketches['__profile__'])
    #del mdb.models['Model-1'].sketches['__profile__']
    mdb.models['Model-1'].parts['Part-1'].HoleThruAllFromEdges(diameter=r,
    distance1=x0, distance2=y0, edge1=
    mdb.models['Model-1'].parts['Part-1'].edges[7+2*i], edge2=
    mdb.models['Model-1'].parts['Part-1'].edges[10+2*i], plane=
    mdb.models['Model-1'].parts['Part-1'].faces[4+i], planeSide=SIDE1)
    i=i+1
#

最佳答案

python - 平板上的随机孔-LMLPHP

下面的代码似乎有效。如果您不希望任何孔相交,则需要更多的编码。

from part import *
from sketch import *
import random

# --- User defined variables------------------------
width=100
height=50
t=8
radius=5.0
minBorder=5.0
numberOfHoles=10
# --------------------------------------------------

try:
    currentModel=mdb.models[session.viewports[session.currentViewportName].displayedObject.modelName]
except:  #if no object or part is visible in the Viewport, then the currentModel will default to Model-1
    currentModel=mdb.models['Model-1']

skethc1=currentModel.ConstrainedSketch(name='__profile__', sheetSize=200.0)
skethc1.rectangle(point1=(0.0, 0.0), point2=(width, height))
part=currentModel.Part(dimensionality=THREE_D, name='Part-1', type=
    DEFORMABLE_BODY)

part.BaseSolidExtrude(depth=t, sketch=skethc1)


#Create a plane and an axis to help setup the sketch consistnely.
# Best to do this over selecting a face and edge number because these numbers change when changing the part.
plane=part.DatumPlaneByOffset(flip=SIDE1, offset=0.0, plane=part.faces[4])
axis=part.DatumAxisByTwoPoint(point1=
    part.vertices[4], point2=part.vertices[6])

for i in range(numberOfHoles):
    # Create a new sketch for each hole
    sketch=currentModel.ConstrainedSketch(gridSpacing=5.6, name='__profile__',
        sheetSize=224.17, transform=part.MakeSketchTransform(sketchPlane=part.datums[plane.id],
        sketchPlaneSide=SIDE1, sketchUpEdge=part.datums[axis.id],
        sketchOrientation=RIGHT, origin=(0.0, 0.0, 0.0)))

    x0=random.uniform(radius+minBorder, width-(radius+minBorder))
    y0=random.uniform(radius+minBorder,height-(radius+minBorder))
    sketch.CircleByCenterPerimeter(center=(x0, y0), point1=(x0,y0+radius))
#
    #Cut this model for each hole because otherwise the sketch might having overlapping edges which wont work.
    part.CutExtrude(flipExtrudeDirection=OFF,
        sketch=sketch, sketchOrientation=RIGHT, sketchPlane=part.datums[plane.id],
        sketchPlaneSide=SIDE1, sketchUpEdge=part.datums[axis.id])

    del currentModel.sketches['__profile__']


编辑:下面的代码可以防止孔重叠。

python - 平板上的随机孔-LMLPHP

from part import *
from sketch import *
import random
import numpy as np


# --- User defined variables------------------------
width=100
height=50
depth=8
radius=5.0
minBorder=5.0
minDistanceBetweenHoleEdges=1
numberOfHoles=10
intersectHoles=False   #If false then the holes will not overlap
# --------------------------------------------------

try:
    currentModel=mdb.models[session.viewports[session.currentViewportName].displayedObject.modelName]
except:  #if no object or part is visible in the Viewport, then the currentModel will default to Model-1
    currentModel=mdb.models['Model-1']

skethc1=currentModel.ConstrainedSketch(name='__profile__', sheetSize=200.0)
skethc1.rectangle(point1=(0.0, 0.0), point2=(width, height))
part=currentModel.Part(dimensionality=THREE_D, name='Part-1', type=
    DEFORMABLE_BODY)

part.BaseSolidExtrude(depth=depth, sketch=skethc1)


#Create a plane and an axis to help setup the sketch consistnely.
# Best to do this over selecting a face and edge number because these numbers change when changing the part.
plane=part.DatumPlaneByOffset(flip=SIDE1, offset=0.0, plane=part.faces[4])
axis=part.DatumAxisByTwoPoint(point1=part.vertices[4], point2=part.vertices[6])

previousLocations=np.ones((numberOfHoles,2))*-99999

for i in range(numberOfHoles):
    # Create a new sketch for each hole
    sketch=currentModel.ConstrainedSketch(gridSpacing=5.6, name='__profile__',
        sheetSize=224.17, transform=part.MakeSketchTransform(sketchPlane=part.datums[plane.id],
        sketchPlaneSide=SIDE1, sketchUpEdge=part.datums[axis.id],
        sketchOrientation=RIGHT, origin=(0.0, 0.0, 0.0)))


    carryOn=True
    while carryOn:
        x0=random.uniform(radius+minBorder, width-(radius+minBorder))
        y0=random.uniform(radius+minBorder,height-(radius+minBorder))

        location=np.array([x0, y0])

        distances=np.sum((previousLocations-location)**2,axis=1)**.5
        if distances.min() >= minDistanceBetweenHoleEdges+radius*2 or intersectHoles:
            carryOn=False


    previousLocations[i,:]= location

    sketch.CircleByCenterPerimeter(center=(x0, y0), point1=(x0,y0+radius))
#
    #Cut this model for each hole because otherwise the sketch might having overlapping edges which wont work.
    part.CutExtrude(flipExtrudeDirection=OFF,
        sketch=sketch, sketchOrientation=RIGHT, sketchPlane=part.datums[plane.id],
        sketchPlaneSide=SIDE1, sketchUpEdge=part.datums[axis.id])

    del currentModel.sketches['__profile__']

关于python - 平板上的随机孔,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33056599/

10-12 17:32