下面的代码似乎工作。如果您不希望任何孔相交,则需要更多编码。
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__']