Moderator: bernd
Code: Select all
import FreeCADGui, FreeCAD
import numpy as np
locations = []
#Variables
num=3 # Number of nearest neigbours to found
Slope = 1000000.000000 # Contact stiffness
Friction = 0.300000 # Friction
Scale = 1 # Scale
def find_index_of_nearest(array, idx,num ):
xpoint=array[idx,0]
ypoint=array[idx,1]
zpoint=array[idx,2]
distance = []
for i in range(array.shape[0]):
distance.append((array[i,0]-xpoint)**2 + (array[i,1]-ypoint)**2+ (array[i,2]-zpoint)**2)
idxmin = np.argpartition(np.array(distance), num)
idxmin=np.array(idxmin[0:num])
idxmin=np.delete(idxmin,np.where(idxmin==idx))
return idxmin
def get_all_close_surfaces(array, num):
store = []
for i in range(array.shape[0]):
store.append(find_index_of_nearest(array,i,num))
return store
def addcontactobjects(array,geom):
[col,row]=np.array(array.shape)
for i in range(array.size):
#print(i)
FreeCAD.activeDocument().addObject("Fem::ConstraintContact","FemConstraintContact")
idxc=0
idxr=0
for obj in FreeCAD.ActiveDocument.Objects:
if (obj.isDerivedFrom('Fem::ConstraintContact')):
objName = obj.Name
#print(objName)
obj.Slope = Slope
obj.Friction = Friction
obj.Scale = Scale
face1='Face'+str(idxc+1)
# print(face1)
face2='Face'+str(array[idxc,idxr]+1)
# print(face2)
obj.References = [(geom,face1), (geom,face2)]
# print(idxc+1,array[idxc,idxr]+)
idxr+=1
if idxr > row-1:
idxr = 0
idxc+=1
try:
for obj in FreeCAD.ActiveDocument.Objects: # seach all objects in document
objName = obj.Name
objLabel = obj.Label
try:
if obj.Shape.Faces: # is a face then
print( objLabel, objName, len(obj.Shape.Faces), " face(s)")
FreeCADGui.Selection.addSelection(FreeCAD.ActiveDocument.getObject(obj.Name)) # select the object with face
if 'Compound' in objName:
print('Getting vector list for nearest neighbours contact \n ', objName)
for face in range(len(obj.Shape.Faces)): # list the face(s) of object
# print(face)
surfaceloc=np.array(obj.Shape.Faces[face].CenterOfMass) # center point
locations.append(surfaceloc)
# print(surfaceloc)
locations = np.array(locations)
#print(locations.shape)
results = np.array(get_all_close_surfaces(locations,num))
# print(results)
addcontactobjects(results,obj)
else:
print("No object found to apply contact ",objName)
except Exception:
print("Not Shape")
except Exception:
print("Not object")
https://www.ansys.com/products/structur ... assembliesI do not get the point of this. What is this feature useful for?
The only input the script need is :What do I need to do to test all this? May be than I have a even better idea
Code: Select all
Slope = 1000000.000000 # Contact stiffness
Friction = 0.300000 # Friction