using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System.Data;
using System.Data.SqlClient;
using System;
using System.Collections.Generic;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
namespace CADDB
{
public class DBLoadUtil //Load all the (With Attributes) Object into Database
public string LoadNOVBlocsWithAttrinutes()
{
string result = "";
SqlConnection conn = DBUtil.GetConnection();
try
{
//get the document and the editor object
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
TypedValue[] tv = new TypedValue[1];
tv.SetValue(new TypedValue((int)DxfCode.Start, "INSERT"), 0); // This is used to filter all Blocks
SelectionFilter filter = new SelectionFilter(tv);
PromptSelectionResult ssPrompt = ed.SelectAll(filter);
//Check to see if there are objects selected
if (ssPrompt.Status == PromptStatus.OK)
{
double insPtX = 0.0, insPtY = 0.0;
string blkName = "";
string layer = "";
string TAGNO= "";
string DWGDESC = "";
string RSONEID_PN = "";
string DESCRIPTION = "";
//string SYSTEM_REF = "";
BlockReference blk;
string insPt = "";
string attributes = "";
SelectionSet ss = ssPrompt.Value;
//For later implementation string Pair_Conductor = "", DAQ_Ch = "", ISA_Tag_No = "", Cable_Tag_Num = "", EG_TITLE2 = ""
//double rotation = 0.0;
//String sql = @"Insert INTO dbo.NOVBlockWithAttributes
// (InsertionPt, BlockName, Layer, TAGNO, DWGDESC, RSONEID_PN, DESCRIPTION, SYSTEM_REF, Attributes, Created)
// VALUES
// (@InsertionPt, @BlockName, @Anonymous, @TagNo, @DwgDesc, @RSoneID_PN, @Description, @SystemRef, @Attributes, @Created)";
String sql = @"Insert INTO dbo.NOVBlockWithAttributes
(InsertionPt, BlockName, Layer, TAGNO, DWGDESC, RSONEID_PN, DESCRIPTION, Attributes, Created)
VALUES
(@InsertionPt, @BlockName, @Anonymous, @TagNo, @DwgDesc, @RSoneID_PN, @Description, @Attributes, @Created)";
//for later implementation Pair_Conductor, DAQ_Ch, ISA_Tag_No, Cable_Tag_Num, EG_TITLE2, Rotation,);
// for later inplementaion @Anonymous_Conductor, @DAQ_Ch, @Anonymous_Tag_No, @Cable_Tag_Num, @eg_TITLE2, @Rotation)";
conn.Open();
// Loop through selection set and insert into DB one Block Object at a time
foreach (SelectedObject sObj in ss)
{
blk = trans.GetObject(sObj.ObjectId, OpenMode.ForRead) as BlockReference;
if (blk.AttributeCollection.Count > 0 & blk.Name.Contains("*")) //Attributes count greater then 0
{
insPtX = blk.Position.X;
insPtY = blk.Position.Y;
insPt = insPtX.ToString() + "," + insPtY.ToString();
blkName = blk.Name;
//Later implementation
//blkName = blk.BlockName;
//blkName = blk.DynamicBlockTableRecord.ToString();
layer = blk.Layer;
//Later implementation
//rotation = blk.Rotation;
// loop through the Block attributes
foreach (ObjectId attRefId in blk.AttributeCollection)
{
DBObject obj = trans.GetObject(attRefId, OpenMode.ForRead);
AttributeReference attRef = obj as AttributeReference;
if (attRef != null)
{
// Check if the attribute is invisible
bool isVisible = attRef.Invisible;
// Check if the attribute is constant
bool isConstant = attRef.IsConstant;
// You can use these properties in your logic
// For example, process only invisible attributes
if (isVisible||isConstant)
{
switch (attRef.Tag)
{
case "RSONEID_PN":
RSONEID_PN = attRef.TextString;
ed.WriteMessage($"RSONE_PN: {RSONEID_PN}\n");
break;
case "DESCRIPTION":
DESCRIPTION = attRef.TextString;
ed.WriteMessage($"DESCRIPTION: {DESCRIPTION}\n");
break;
}
// Process the invisible attribute
}
//// For example, process only constant attributes
//if (isConstant)
//{
// // Process the constant attribute
// switch (attRef.Tag)
// {
// case "RSONEID_PN":
// RSONEID_PN = attRef.TextString;
// ed.WriteMessage($"RSONE_PN: {RSONEID_PN}\n");
// break;
// case "DESCRIPTION":
// DESCRIPTION = attRef.TextString;
// ed.WriteMessage($"DESCRIPTION: {DESCRIPTION}\n");
// break;
// }
//}
// Assign values to variables based on attribute tags
switch (attRef.Tag)
{
case "TAGNO":
TAGNO = attRef.TextString;
break;
case "DWGDESC":
DWGDESC = attRef.TextString;
break;
case "RSONEID_PN":
RSONEID_PN = attRef.TextString;
break;
case "DESCRIPTION":
DESCRIPTION = attRef.TextString;
break;
//case "SYSTEM_REF":
// SYSTEM_REF = attRef.TextString;
// break;
//below for futute implementation
//case "Pair_Conductor":
// Pair_Conductor = attRef.TextString;
// break;
//case "DAQ_Ch":
// DAQ_Ch = attRef.TextString;
// break;
//case "ISA_Tag_No":
// ISA_Tag_No = attRef.TextString;
// break;
//case "Cable_Tag_Num":
// Cable_Tag_Num = attRef.TextString;
// break;
//case "EG_TITLE2":
// EG_TITLE2 = attRef.TextString;
// break;
// Add cases for other attributes
// ...
}
attributes += attRef.Tag + "=" + attRef.TextString + ",";
}
}
attributes = attributes.Substring(0, attributes.Length - 1);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@InsertionPT", insPt);
cmd.Parameters.AddWithValue("@BlockName", blkName);
cmd.Parameters.AddWithValue("@Layer", layer);
cmd.Parameters.AddWithValue("@TagNo", TAGNO);
cmd.Parameters.AddWithValue("@DWGDESC", DWGDESC);
cmd.Parameters.AddWithValue("@RSONEID_PN", RSONEID_PN);
cmd.Parameters.AddWithValue("@Description", DESCRIPTION);
//cmd.Parameters.AddWithValue("@SystemRef", SYSTEM_REF);
cmd.Parameters.AddWithValue("@Attributes", attributes);
cmd.Parameters.AddWithValue("@Created", DateTime.Now);
// future implementation
//cmd.Parameters.AddWithValue("@Pair_Conductor", Pair_Conductor);
//cmd.Parameters.AddWithValue("@DAQ_Ch", DAQ_Ch);
//cmd.Parameters.AddWithValue("@ISA_Tag_No", ISA_Tag_No);
//cmd.Parameters.AddWithValue("@Cable_Tag_Num", Cable_Tag_Num);
//cmd.Parameters.AddWithValue("@EG_TITLE2", EG_TITLE2);
//cmd.Parameters.AddWithValue("@Rotation", rotation);
cmd.ExecuteNonQuery();
attributes = "";
}
}
}
else
{
ed.WriteMessage("No Object Is Selected");
}
result = "Done. Completed Successfully";
}
}
catch (Exception ex)
{
result = ex.Message;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return result;
}