CAD利用python开发——自动阵列绘制两排间距不一致的圆避免重合

liftword3周前 (05-28)技术文章7

# 一.程序解决的问题

实现间距不同的两排阵列孔,不重叠;

# 二.实现步骤

1.设定两排阵列孔各自间距;

2.确定两排阵列孔最小间距,避免重叠并保持适当距离;

3.先画好第一排孔,在画第二排孔,画第二排孔时,每次都判断一次距离最近的第二排孔是否已经画好,或者间距是否超出了间距,而跳出此次循环,进行下一次循环;

4.画孔时,先画间距较大的孔

# 三.代码


Distance3 = 300             #第一排孔距300mm,此圆直径10mm
Distance10=120              #第二排孔距120mm,此圆直径4mm
Distance11=23               #两排孔距离23mm
height1 =15
height2 =15
BendsAllowance1 =1.8
Distance9=59 
Distance2 = 10 
point_x =150
lenth =1560
width=587
#上述常量可自己定义,或根据需要取消
# 计算指定长度长度需要打多少第一排孔,round实现四舍五入
    num1 = round((lenth - Distance9 * 2) / Distance3)
    # 当长度小于lenth - Distance9 * 2时,中间不加螺钉孔
    if num1==0 or num1==1:
        lenth1= (lenth - Distance9 * 2)
        num1=1
    else:
        lenth1 = (lenth - Distance9 * 2) / num1
    # 计算指定长度长度需要打多少第二排孔
    num2 = int((lenth - Distance9 * 2) / (Distance10))
    if num2 ==0 or num2==1:
        distan2 = (lenth - Distance9 * 2)
        num2=1
    else:
        distan2 = (lenth - Distance9 * 2) / num2
#画第一排第一个圆
	distance4 =  height1 + height2 - BendsAllowance1 * 2 + Distance9
	center1, radius1 = vtpnt(distance4, point_x +width / 2 - Distance2 - BendsAllowance1 / 2, 0), 5
    circleObj_5 = msp.AddCircle(center1, radius1)
# 画第一排第一个圆并阵列第一排圆
	numberOfRows = 2
	numberOfColumns = num1+1
	numberOfLevels = 1
	# 螺钉过孔行间距
	distanceBwtnRows_1 = -(width / 2 - Distance2 - BendsAllowance1 / 2) * 2
	distanceBwtnColumns = lenth1
	distanceBwtnLevels = 1
	
	retObj_1 = circleObj_5.ArrayRectangular(numberOfRows, numberOfColumns, numberOfLevels,
	                                        distanceBwtnRows_1, distanceBwtnColumns, distanceBwtnLevels)
 # 画第二排第一个圆
 	numberOfRows = 2
    numberOfColumns = num2+1
    numberOfColumns_1=1
    numberOfColumns_2 = 2
    numberOfLevels = 1
    distanceBwtnRows = -(width / 2 + height1 + Distance2 - BendsAllowance1 * 2) * 2
    # 第一个铆钉孔行间距
    distanceBwtnRows_1 = -(width / 2 + height1 + Distance2 - BendsAllowance1 * 2-Distance4) * 2
 #循环判断第二排圆与第一排圆的距离,避免相互干涉
    for i in range(1,num2):
        for j in range(1,num1+1):
            print(abs((j*lenth1)-(i*distan2+Distance11)))
            if abs((j*lenth1)-(i*distan2+Distance11))>Distance5:
                print(abs(j*lenth1-(i*distan2+Distance11)))
                # 当第一排圆距离小于第二排圆距离时及间距大于lenth1时说明满足需求的孔已经画了,开始画下一个孔
                if j*lenth1-(i*distan2)<0 or abs(j*lenth1-(i*distan2+Distance11))>lenth1:
                    continue
                # 第二排孔间距
                distanceBwtnColumns = distan2*i
                # 画第二排第一个圆
                center3_1, radius3_1 = vtpnt(distance4 + Distance11,
                                             point_x + width / 2 + height1 + Distance2 - BendsAllowance1 * 2,
                                             0), 2
                circleObj_8 = msp.AddCircle(center3_1, radius3_1)
                # 阵列第二排圆
                retObj_2 = circleObj_8.ArrayRectangular(numberOfRows, numberOfColumns_2, numberOfLevels,
                                                        distanceBwtnRows, distanceBwtnColumns, distanceBwtnLevels)
            else:
                # 当第一排圆与第二排圆间距小于8mm时候,把第二排圆增加16mm
                distanceBwtnColumns = distan2 * i+16
                #画第二排第一个圆
                center3_1, radius3_1 = vtpnt(distance4 + Distance11,
                                             point_x + width / 2 + height1 + Distance2 - BendsAllowance1 * 2,
                                             0), 2
                circleObj_8 = msp.AddCircle(center3_1, radius3_1)
                # 阵列第二排圆
                retObj_2 = circleObj_8.ArrayRectangular(numberOfRows, numberOfColumns_2, numberOfLevels,
                                                        distanceBwtnRows, distanceBwtnColumns, distanceBwtnLevels)
                break
```

# 四.结果

两排圆的起点距离不一致

相关文章

Python算法:4.寻找两个正序数组的中位数

题目:寻找两个正序数组的中位数给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。 算法的时间复杂度应该为 O(log(m+n...

在Python中怎么用二分法插入一个新的数到数组列表

接昨天的把一个数插入到有序的数组、列表中,我们今天使用二分法来,就好比一个队分成2个队,因为有序的么,先比较中间的,得到结果,只要比较一个方向的一半就可以了,这样一来是不是减少一半的时间,节约是运行时...

【找出两个整数数组中同时出现的整数】Python实现

from collections import Counter def find_common_elements(arr1, arr2): # 统计数组中每个数字的出现次数 coun...

快速掌握Python数组的逆序输出几种方法

在 Python 中,可以通过多种方法实现数组的逆序输出。我用以下是几种常见的方法来讲讲,更多的方法方式就考自己摸索、探究。方式1:最容易想到的是使用切片通过 [::-1] 切片语法直接逆序列表:nu...

Python NumPy 数组形状操作

在 Python 的 NumPy 库中,数组的形状(shape)是指数组的维度和每个维度的大小。数组形状操作是一项重要的功能,可以更改数组的维度和大小。本主要介绍Python NumPy的数组形状操作...

einsum函数多维数组处理指南

高效处理多维数组:einsum()函数从入门到精通einsum(爱因斯坦求和约定)是处理多维数组的终极利器,它能用简洁的符号表达复杂的线性代数运算。本文将带你从基础语法到高阶应用全面掌握这个神奇的函数...