using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
namespace ITAT_Autocad
{
public class Format
{
private string formatID;
private int multiplier;
private double height;
private double width;
private string orientation;
private string type;
public string FormatID { get => formatID; set => formatID=value; }
public int Multiplier { get => multiplier; set => multiplier=value; }
public double Height { get => height; set => height=value; }
public double Width { get => width; set => width=value; }
public string Orientation { get => orientation; set => orientation=value; }
public string Type { get => type; set => type=value; }
}
public class GOST_Titles
{
static double curWidth;
static double curHeight;
private static Point2d frPoint1;
private static Point2d frPoint2;
private static Point2d frPoint3;
private static Point2d frPoint4;
private static Point2d keyPoint1;
private static Point2d keyPoint2;
private static Point2d keyPoint3;
private static Point2d keyPoint4;
public static List<Format> listOfFormats = new List<Format>
{
new Format { FormatID = "A4", Height = 297, Width = 210 },
new Format { FormatID = "A3", Height = 420, Width = 297 },
new Format { FormatID = "A2", Height = 594, Width = 420 },
new Format { FormatID = "A1", Height = 841, Width = 594 },
new Format { FormatID = "A0", Height = 1189, Width = 841 }
};
public static Format GetDataFromUser ()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
//Выбрать формат
PromptKeywordOptions chFormat = new PromptKeywordOptions("\nChoose format: ");
chFormat.Keywords.Add("A4");
chFormat.Keywords.Add("A3");
chFormat.Keywords.Add("A2");
chFormat.Keywords.Add("A1");
chFormat.Keywords.Add("A0");
chFormat.AllowNone=false;
PromptResult formatID = acDoc.Editor.GetKeywords(chFormat);
//Application.ShowAlertDialog("The name entered was: "+formatID.StringResult);
//Установить кратность
PromptStringOptions chMultiplier = new PromptStringOptions("\nSet multiplier: ");
chMultiplier.AllowSpaces=false;
PromptResult multiplier = acDoc.Editor.GetString(chMultiplier);
// Установить ориентацию
PromptKeywordOptions chOrient = new PromptKeywordOptions("\nSet orientation: ");
chOrient.Keywords.Add("Portrait");
chOrient.Keywords.Add("Landscape");
chOrient.Keywords.Default="Landscape";
chOrient.AllowNone=false;
PromptResult orientation = acDoc.Editor.GetKeywords(chOrient);
// Установить тип
PromptKeywordOptions chType = new PromptKeywordOptions("\nSet type: ");
chType.Keywords.Add("Title");
chType.Keywords.Add("Regular");
chType.Keywords.Default="Regular";
chType.AllowNone=false;
PromptResult type = acDoc.Editor.GetKeywords(chType);
Format curFormat = new Format
{
FormatID=formatID.StringResult,
Multiplier=int.Parse(multiplier.StringResult),
Orientation=orientation.StringResult,
Type=type.StringResult,
};
return curFormat;
}
public static Format inFormat = GetDataFromUser();
[CommandMethod("2InsertTitle")]
public static void DrawFormat ()
{
Format curFormat = inFormat;
if ( curFormat.FormatID=="A4" )
{
if ( curFormat.Orientation=="Portrait" )
{
curHeight=listOfFormats[ 0 ].Height;
curWidth=listOfFormats[ 0 ].Width*curFormat.Multiplier;
}
else
{
curHeight=listOfFormats[ 0 ].Width;
curWidth=listOfFormats[ 0 ].Height;
}
}
if ( curFormat.FormatID=="A3" )
{
if ( curFormat.Orientation=="Portrait" )
{
curHeight=listOfFormats[ 1 ].Height;
curWidth=listOfFormats[ 1 ].Width*curFormat.Multiplier;
}
else
{
curHeight=listOfFormats[ 1 ].Width;
curWidth=listOfFormats[ 1 ].Height;
}
}
if ( curFormat.FormatID=="A2" )
{
if ( curFormat.Orientation=="Portrait" )
{
curHeight=listOfFormats[ 2 ].Height;
curWidth=listOfFormats[ 2 ].Width*curFormat.Multiplier;
}
else
{
curHeight=listOfFormats[ 2 ].Width;
curWidth=listOfFormats[ 2 ].Height;
}
}
if ( curFormat.FormatID=="A1" )
{
if ( curFormat.Orientation=="Portrait" )
{
curHeight=listOfFormats[ 3 ].Height;
curWidth=listOfFormats[ 3 ].Width*curFormat.Multiplier;
}
else
{
curHeight=listOfFormats[ 3 ].Width;
curWidth=listOfFormats[ 3 ].Height;
}
}
if ( curFormat.FormatID=="A0" )
{
if ( curFormat.Orientation=="Portrait" )
{
curHeight=listOfFormats[ 4 ].Height;
curWidth=listOfFormats[ 4 ].Width*curFormat.Multiplier;
}
else
{
curHeight=listOfFormats[ 4 ].Width;
curWidth=listOfFormats[ 4 ].Height;
}
}
frPoint1=new Point2d(-curWidth+5, curHeight-5);
frPoint2=new Point2d(5, curHeight-5);
frPoint3=new Point2d(5, -5);
frPoint4=new Point2d(-curWidth+5, -5);
keyPoint1=new Point2d(-curWidth+25, curHeight-10);
keyPoint2=new Point2d(0, curHeight-10);
keyPoint3=new Point2d(0, 0);
keyPoint4=new Point2d(-curWidth+25, 0);
if ( curFormat.Type=="Regular" ) { DrawR(); }
else { DrawM(); }
}
public static void DrawM ()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using ( Transaction acTrans = acCurDb.TransactionManager.StartTransaction() )
{
LayerTable acLyrTbl;
acLyrTbl=acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
string LayerName = "-10-Formats";
if ( acLyrTbl.Has(LayerName)==false )
using ( LayerTableRecord acLyrTblRec = new LayerTableRecord() )
{
// Assign the layer the ACI color 3 and a name
acLyrTblRec.Color=Color.FromColorIndex(ColorMethod.ByAci, 7);
acLyrTblRec.Name=LayerName;
// Upgrade the Layer table for write
acLyrTbl.UpgradeOpen();
// Append the new layer to the Layer table and the transaction
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
//acCurDb.Clayer=acLyrTbl[ LayerName ];
BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if ( !acBlkTbl.Has("MainTitle") )
{
BlockTableRecord acBlkTblRec = new BlockTableRecord();
acBlkTblRec.Name="MainTitle";
// Set the insertion point for the block
acBlkTblRec.Origin=new Point3d(0, 0, 0);
acBlkTbl.UpgradeOpen();
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
//Draw frontiers of format
DrawFrames(acTrans, acBlkTblRec);
//Draw upper title of format
DrawUpperTitle(acTrans, acBlkTbl, acBlkTblRec, inFormat);
//Draw main title of format
DrawMainTitle(acTrans, acBlkTbl, acBlkTblRec);
//DrawRegularTitle(acTrans, acBlkTbl, acBlkTblRec);
DrawSideTitle(acTrans, acBlkTbl, acBlkTblRec);
blkRecId=acBlkTblRec.Id;
}
else
{
blkRecId=acBlkTbl[ "MainTitle"+" "+LayoutManager.Current.CurrentLayout];
}
// Insert the block into the current space
if ( blkRecId!=ObjectId.Null )
{
BlockTableRecord acBlkTblRec;
acBlkTblRec=acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
// Create and insert the new block reference
BlockReference acBlkRef = new BlockReference(acBlkTblRec.Origin, blkRecId);
acBlkRef.SetDatabaseDefaults();
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec=
acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
// Verify block table record has attribute definitions associated with it
if ( acBlkTblRec.HasAttributeDefinitions )
foreach ( ObjectId objID in acBlkTblRec )
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead);
if ( dbObj is AttributeDefinition )
{
AttributeDefinition acAtt = dbObj as AttributeDefinition;
if ( !acAtt.Constant )
{
AttributeReference acAttRef = new AttributeReference();
acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
//acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
acAttRef.TextString=acAtt.TextString;
acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}
// Save the new object to the database
acTrans.Commit();
}
}
public static void DrawR ()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using ( Transaction acTrans = acCurDb.TransactionManager.StartTransaction() )
{
LayerTable acLyrTbl;
acLyrTbl=acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
string LayerName = "-10-Formats";
if ( acLyrTbl.Has(LayerName)==false )
using ( LayerTableRecord acLyrTblRec = new LayerTableRecord() )
{
// Assign the layer the ACI color 3 and a name
acLyrTblRec.Color=Color.FromColorIndex(ColorMethod.ByAci, 7);
acLyrTblRec.Name=LayerName;
// Upgrade the Layer table for write
acLyrTbl.UpgradeOpen();
// Append the new layer to the Layer table and the transaction
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
//acCurDb.Clayer=acLyrTbl[ LayerName ];
BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if ( !acBlkTbl.Has("RegularTitle") )
{
BlockTableRecord acBlkTblRec = new BlockTableRecord();
acBlkTblRec.Name="RegularTitle";
// Set the insertion point for the block
acBlkTblRec.Origin=new Point3d(0, 0, 0);
acBlkTbl.UpgradeOpen();
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
//Draw frontiers of format
DrawFrames(acTrans, acBlkTblRec);
//Draw upper title of format
DrawUpperTitle(acTrans, acBlkTbl, acBlkTblRec, inFormat);
//Draw main title of format
//DrawMainTitle(acTrans, acBlkTbl, acBlkTblRec);
DrawRegularTitle(acTrans, acBlkTbl, acBlkTblRec);
DrawSideTitle(acTrans, acBlkTbl, acBlkTblRec);
blkRecId=acBlkTblRec.Id;
}
else
{
blkRecId=acBlkTbl[ "RegularTitle" + " " + LayoutManager.Current.CurrentLayout];
}
// Insert the block into the current space
if ( blkRecId!=ObjectId.Null )
{
BlockTableRecord acBlkTblRec;
acBlkTblRec=acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
// Create and insert the new block reference
BlockReference acBlkRef = new BlockReference(acBlkTblRec.Origin, blkRecId);
acBlkRef.SetDatabaseDefaults();
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec=
acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
// Verify block table record has attribute definitions associated with it
if ( acBlkTblRec.HasAttributeDefinitions )
foreach ( ObjectId objID in acBlkTblRec )
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead);
if ( dbObj is AttributeDefinition )
{
AttributeDefinition acAtt = dbObj as AttributeDefinition;
if ( !acAtt.Constant )
{
AttributeReference acAttRef = new AttributeReference();
acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
//acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
acAttRef.TextString=acAtt.TextString;
acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}
// Save the new object to the database
acTrans.Commit();
}
}
public static void DrawMainTitle ( Transaction acTrans, BlockTable acBlkTbl, BlockTableRecord acBlkTblRec )
{
тут код метода
}
public static void DrawRegularTitle ( Transaction acTrans, BlockTable acBlkTbl, BlockTableRecord acBlkTblRec )
{
тут код метода
}
public static void DrawUpperTitle ( Transaction acTrans, BlockTable acBlkTbl, BlockTableRecord acBlkTblRec, Format inFormat )
{
Format initFormat = new Format()
{
Orientation=inFormat.Orientation,
Multiplier=inFormat.Multiplier,
};
if ( initFormat.Orientation=="Portrait"&&initFormat.Multiplier==1 )
{
тут код метода
}
}
else
{
тут код метода
}
}
}
public static void DrawSideTitle ( Transaction acTrans, BlockTable acBlkTbl, BlockTableRecord acBlkTblRec )
{
тут код метода
}
public static void DrawFrames ( Transaction acTrans, BlockTableRecord acBlkTblRec )
{
тут код метода
}
}
}