这是我在平板上建模一些随机孔的py代码。为什么此代码报告以下错误:(IndexError: Sequence index out of range)
?我该如何修理它?
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
#
答案 0 :(得分:1)
以下代码似乎有效。如果你不想让任何一个洞相交,那就需要更多的编码。
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__']
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__']