[C#] CAD Fillet 기능 구현 예시 [도형의 모서리를 둥글게 하는 작업]

2021. 1. 18. 10:03C#

사용한 Visual Studio 환경

더보기

Visual Studio Community 2019 

vs_community__468244237.1605342870

 

CAD의 Fillet 기능 : 도형의 꼭지점을 반지름만큼 둥글게 하는 작업

 

C#으로 구현하기

 

Rectangle Point Location

 

Graphics Point Explanation

 

Run Screen

 

C# Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.panel1.Controls.Clear();

            Graphics g = this.panel1.CreateGraphics();
            Pen p0 = new Pen(Color.Aquamarine, 3);
            Pen p1 = new Pen(Color.Pink, 3);
            Pen p2 = new Pen(Color.Red, 3);
            Pen p3 = new Pen(Color.Violet, 3);
            Pen p4 = new Pen(Color.DarkGray, 3);

            int width = int.Parse(textBox1.Text);
            int height = int.Parse(textBox2.Text);
            int radius = int.Parse(textBox3.Text);
            
            Rectangle rec = new Rectangle(10, 10, width, height);

            float stAngle = 180;
            float endAngle = 90;

            g.DrawArc(p0, new Rectangle(10, 10, radius, radius), stAngle, endAngle);
            g.DrawLine(p1, new Point(rec.X, rec.Top + radius - (radius / 2)), new Point(rec.X, rec.Top + height)) ;
            g.DrawLine(p2, new Point(rec.X, rec.Top + height), new Point(rec.X + width, rec.Top + height));
            g.DrawLine(p3, new Point(rec.X + width, rec.Top + height), new Point(rec.X + width, rec.Top));
            g.DrawLine(p4, new Point(rec.X + width, rec.Top), new Point(rec.X + radius - (radius / 2), rec.Top));

        }

    }



 }

위 그림은 왼쪽 상단만 행한 것.

1. 원하는 지점의 꼭지점에서 원하는 반경 만큼 Fillet을 한다.

    = Arc 를 사용해서 호를 그려준다.

2. 베이스가 된 사각형을 그려주면 된다.

    = Line을 사용해서  (Arc 된 지점 - 사각형의 다른 꼭지점) 의 지점을 그려주면 된다

 

Pen Information

 

C# Code_Designer

더보기
namespace WindowsFormsApp1
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(116, 159);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(94, 29);
            this.button1.TabIndex = 0;
            this.button1.Text = "make";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(32, 40);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(63, 20);
            this.label1.TabIndex = 1;
            this.label1.Text = "Width : ";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(27, 73);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(68, 20);
            this.label2.TabIndex = 2;
            this.label2.Text = "Height : ";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(15, 108);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(79, 20);
            this.label3.TabIndex = 3;
            this.label3.Text = "Diameter :";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(100, 37);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(110, 27);
            this.textBox1.TabIndex = 4;
            this.textBox1.Text = "200";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(100, 73);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(110, 27);
            this.textBox2.TabIndex = 5;
            this.textBox2.Text = "200";
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(100, 108);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(110, 27);
            this.textBox3.TabIndex = 6;
            this.textBox3.Text = "100";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.textBox2);
            this.groupBox1.Controls.Add(this.button1);
            this.groupBox1.Controls.Add(this.textBox3);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.textBox1);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Location = new System.Drawing.Point(557, 21);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(231, 201);
            this.groupBox1.TabIndex = 7;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Info";
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.SystemColors.Window;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(543, 452);
            this.panel1.TabIndex = 8;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Panel panel1;
    }
}

 

사각형의 모든 면이 Fillet 되는 예시가 필요한 사람은 아래를 참조하면 된다. (Designer 코드는 동일하다)

더보기

 

실행 이미지

 

Code 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.panel1.Controls.Clear();

            Graphics g = this.panel1.CreateGraphics();
            Pen p0 = new Pen(Color.Aquamarine, 3);
            Pen p1 = new Pen(Color.Pink, 3);
            Pen p2 = new Pen(Color.Red, 3);
            Pen p3 = new Pen(Color.Violet, 3);
            Pen p4 = new Pen(Color.DarkGray, 3);

            int width = int.Parse(textBox1.Text);
            int height = int.Parse(textBox2.Text);
            int radius = int.Parse(textBox3.Text);
            
            Rectangle rec = new Rectangle(10, 10, width, height);

            float stAngle = 180;
            float endAngle = 90;

            g.DrawArc(p0, new Rectangle(10, 10, radius, radius), stAngle, endAngle);
            g.DrawArc(p0, new Rectangle(rec.X + radius, 10, radius, radius), (float)270, (float)90);
            g.DrawArc(p0, new Rectangle(rec.X+radius, 10+radius, radius, radius), (float)0, (float)90);
            g.DrawArc(p0, new Rectangle(10, 10 + radius, radius, radius), (float)90, (float)90);
            
            g.DrawLine(p1, new Point(rec.X, rec.Top + radius - (radius / 2)), new Point(rec.X, rec.Top + height - (radius / 2))) ;
            g.DrawLine(p2, new Point(rec.X + (radius / 2), rec.Top + height), new Point(rec.X + width - (radius / 2), rec.Top + height));
            g.DrawLine(p3, new Point(rec.X + width, rec.Top + height - (radius / 2)), new Point(rec.X + width, rec.Top + (radius / 2)));
            g.DrawLine(p4, new Point(rec.X + width - (radius / 2), rec.Top), new Point(rec.X + radius - (radius / 2), rec.Top));

        }

    }



 }