隧道通风网络节点图导出CAD文本时的三角形算法

10 篇文章 0 订阅
8 篇文章 4 订阅

在TESP软件中涉及到将带文本的节点图导出为CAD文件,其中文本的绘制需要根据Section的方向来确定,确保和绘图的习惯一致。具体包括:

(1)垂直绘制的Section需确保文字字头向左。

(2)除了垂直的文字,其他文字,不论箭头是向左,还是向右,文字均应在Section的上方。

软件中Swing绘制的通风网络节点图

导出的CAD文件中的文本绘制效果

具体方法:

1、求解section箭头线的准确角度,0-360°;

2、根据代码进行文本绘制。

Swing 中的文本绘制旋转

三角函数

代码实现:

1、箭头线角度计算:

                double detX = x2 - x1;
				double detY = y2 - y1;
				double detL = Math.sqrt(Math.pow(detX, 2) + Math.pow(detY, 2));
				double sinaTheta = detY / detL;
				double cosTheta = detX / detL;
				double theta = 0;
				double theta_Fact=Math.acos(cosTheta);
					//0-180°,如果sina<0,采用acos求角度		
				if(sinaTheta>=0) {
					theta=Math.acos(cosTheta);
					theta_Fact=Math.toDegrees(theta);
				}
				//如果sina<0,
				else if (sinaTheta<0) {
					//180-270°,采用acos求角度	
					if(cosTheta<0) {
						theta=Math.acos(cosTheta);
						theta_Fact=360-Math.toDegrees(theta);
						theta=Math.toRadians(theta_Fact);
					}
					else {
						//270-360°,采用asin求角度	
						theta=Math.asin(sinaTheta);
						theta_Fact=Math.toDegrees(theta)+360;
					}
					
				}

2、文本绘制:

				double sx = 0;
				double sy = 0;
				
				//90°,误差1°,y1<y2,箭头向南,y1<y2;
				if(Math.abs(theta_Fact-90)<1.000)
				{			
				        sy = y2;
						sx = x2;
						theta=-1*theta;
				}
							
				//270°,误差1°,y1>y2,箭头向北
				else if(Math.abs(theta_Fact-270)<1.000) {
					sy = y1;
					sx = x1;
					
				}
				//0-90°	
				else if (theta_Fact<90){
					
						sy = y1;
						sx = x1;

				//如果91-180°
				} else if((theta_Fact<=180)&&(theta_Fact>90)){
					
					sy = y2;
					sx = x2;
					theta_Fact=theta_Fact+180;
					theta=Math.toRadians(theta_Fact);
			//如果181-270°
			} else if((theta_Fact<=270)&&(theta_Fact>180)){
			    sy = y2;
				sx = x2;
				theta_Fact=theta_Fact+180;
				theta=Math.toRadians(theta_Fact);	
				//如果271-360°
			} else if ((theta_Fact<=360)&&(theta_Fact>270)){
				
				sy = y1;
				sx = x1;
				
			}		
				double offset = 5;
				graphics.setColor(Color.GREEN);
				graphics.rotate(theta, sx, sy); // counterclockwise...
				// graphics.shear(-0.4f, -0.3f);

				ILVSection section = (ILVSection) mc.getValue();
				// String title = arrow + " " + section.getSzTitle();
				String title = section.getSzTitle();
				fm = graphics.getFontMetrics();
				width = fm.stringWidth(title);
				graphics.drawString(StringUnicode.stringToUnicode(title), sx + offset, sy - offset);

				// graphics.drawRect(cenX+5, cenY+5, 20, 20);
				// String secID=section.getIiD()+"";
				// fm = graphics.getFontMetrics();
				// ascent = fm.getAscent();
				// descent = fm.getDescent();
				// width = fm.stringWidth(secID);
				// graphics.drawString(secID, cenX - width / 2, cenY +5);
				graphics.rotate(-1 * theta, sx, sy);
			}

		}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值