Draw six capacitors in fxcad Experiment
To draw a capacitor, first create a class file under the fxclasslibrary directory (here we will create a class file named "fxbox_dianrong.cs ), next, create a "Capacitor" button in the toolwindowhost file in the verticaltooltabs file under the fxcontrolle Directory, which includes the resistance graph and node drawing code. Next we will discuss in detail how resistance and nodes and connections are drawn.
1. Create a class file named "fxbox_dianrong.cs" under the fxclasslibrary directory. Set and define some variables to be used to draw the resistance. Here we add the following code:
Privatefonttxtfont; // text
Privatepen linepen; // linear pen used to draw image lines
Privatepen rectpen; // rectangular pen used to draw the border degree when the image is selected
Private color linecolor; // line color
Private color rectcolor; // Rectangle frame color
Private pen linepen_selected; // pen selected
Private color linecolor_selected;
Private pen pointpen; // connection point
Private color pointcolor; // The connection point color. Optional.
2. Associate the resistance file with the resistance button event. Create a button named "Capacitor" in toolwindowhost and add the following code in the button event:
If (frmdesign! = NULL)
{
System. Collections. hashtable ht = new system. Collections. hashtable ();
Ht. Add ("class", "box_dianrong ");
This. frmdesign. createfxlibrarymodelobject (action. creatlibrarymodelobject, HT );
}
Then add the following code to formdesignview. CS:
Case "box_dianrong ":
{
Fxclasslibrary. fxbox_dianrong box = new fxclasslibrary. fxbox_dianrong ();
This. newobject = (fxcad. Core. librarymodelobject. libobject) (box );
Break;
}
3. After you associate a class file with a button event according to steps 1 and 2, You need to draw a "Capacitor" Image in the class file (here we still use fxbox_dianrong.cs as an example, to prepare for drawing the image in the future, you must set and draw the node in the process of drawing the "Capacitor:
①,In fxcad, the main purpose of drawing a "capacitive" image is to achieve the connection between "capacitive" and "capacitive" and other images, therefore, when drawing a "Capacitor" image, you need to define the corresponding node (its coordinate settings refer to the specific "Capacitor" data) and calculate the number of nodes required. Here, we define nodes in the constructor "Capacitor" drawn in fxbox_dianzu.cs, and calculate the number of nodes (in fxbox_dianrong.cs, the number of nodes is calculated by using the function "Init () "). The Code is as follows:
Public fxbox_dianrong ()
{
// Constructor
This. fxboxnodetype = (INT) fxbox_dianrong.light_node_type.general;
Nodes = new arraylist ();
Setname (guid. newguid (). tostring ());/
Nodes. Add (new node ("capacitor a", 0 ));
Nodes. Add (new node ("capacitor B", 0 ));
This. INIT (2 );
}
Private void Init (INT nodenum)
{
Obj_type = (INT) libobject. objecttype. libobject;
Linecolor = color. Black;
Rectcolor = color. Red;
Linepen = new pen (linecolor, 2 );
Rectpen = new pen (rectcolor, 1 );
Rectpen. dashstyle = system. Drawing. drawing2d. dashstyle. Dash; // dashstyle. Dash;
Linecolor_selected = color. Red;
Linepen_selected = newpen (linecolor_selected, 2 );
Pointcolor = color. Red;
Pointpen = new pen (pointcolor, 2 );
Txtfont = new font ("", 12 );
This. nodenum = nodenum; // Save the number of connected points
}
②,Create a drawing function in the fxbox_dianrong.cs File
Public overridevoid drawgraphics (system. Drawing. Graphics g)
{
}
③,The second is the Status Analysis of the connection between the "Capacitor" and other images: To achieve the connection between the resistance, between the capacitor and between the resistance and the capacitor, it is necessary to determine the connection between the resistance, the status between the capacitor and between the resistor and the capacitor (whether connected or selected ). There are three states: Normal, selected, and linking ). Draw different graphs based on this State (the colors of resistors are used here ). To better view the selected image, a function is defined to draw a red dotted line rectangle outside the selected image. The main code is:
Private void drawselectedrect (Graphics g, pen rectpen)
{
// Draw a border (drawn when selected)
G. drawrectangle (rectpen, week X-40, week Y-10, weight + 140, height + 40 );
}
③,To connect "Capacitors" and "Capacitors" to other images, you must set the corresponding node coordinates in the "Capacitor" image. And draw the corresponding "Node" image. The following "Node" function is defined here:
Privatevoid drawnode (Graphics g, pen linepen)
{
}
④,Based on ③ "Capacitor" status analysis, the following figure shows how to implement the "Capacitor" status and node in ① and ②:
Publicoverride void drawgraphics (system. Drawing. Graphics g)
{
Switch (obj_state)
{
Case (INT) libobject. objectstate. Normal:
{
This. drawnode (G, linepen );
Break;
}
Case (INT) libobject. objectstate. Selected:
{
This. drawnode (G, linepen_selected );
This. drawselectedrect (G, rectpen );
Break;
}
Case (INT) libobject. objectstate. Linking:
{
This. drawnode (G, linepen_selected );
This. drawselectedrect (G, rectpen );
Break;
}
}
}
4. capacitive Rendering
The coordinate axes in winform are different from those in normal contact plane. The coordinate axes in winform are in the opposite direction: the upper left corner of the form is the origin (0, 0), and X is increased horizontally to the left, vertical downward direction increases by Y
The following code draws a capacitor:
G. drawline (new pen (color. Red, 2), using X, using Y + 11, using X-30, using Y + 11 );
G. drawline (new pen (color. Red, 2), using X + 20, using Y + 11, using X + 50, using Y + 11 );
G. drawline (new pen (color. Red, 2), using X, using Y, using X, using Y + 25 );
G. drawline (new pen (color. Red, 2), using X + 20, using Y, using X + 20, using Y + 25 );
5. Based on the graphic analysis above (4. Drawing the capacitor), you can set the coordinates of specific nodes in the node functions and draw the corresponding nodes according to the node coordinates. Whether a node is visible depends on whether the image is selected and whether the node is connected by other images, set the node to be drawn based on the selected state of the image and the connection between nodes:
①,First, initialize the node.
Linkpoint_flag = false; // No connection point exists in the initial state.
②,The second is to set and draw Nodes Based on the Resistance and node status.
Node. setlinkpoint (p0.x + X, p0.y + 11); // set the connection point
G. drawrectangle (pointpen, (p0.x + x)-5/2-1, (p0.y + 10)-5/2, 5, 5 );
③,Then, determine whether the node has been connected and set the node based on the judgment.
If (node. linklineobj = NULL) // you can connect a node without any connection.
{
If (MX> node_x0-20 & MX <node_x0 + 20 & my> p0.y + 2 & my <p0.y + 22)
{
Pointnode = node; // a connection point exists in this region
G. fillellipse (brushes. Black, (cost X + x)-5/2-1, (cost Y + 10)-5/2, 5, 5 );
Linkpoint_flag = true; // locate the connection point
}
}
④,Is to save the coordinates of the connection point
Linkpoint. x = P0.X-30;
Linkpoint. Y = 127y + 11;
The program running result is as follows: