Invisible attributes not extracting to SQL database.

Invisible attributes not extracting to SQL database.

rbh2024
Enthusiast Enthusiast
717 Views
5 Replies
Message 1 of 6

Invisible attributes not extracting to SQL database.

rbh2024
Enthusiast
Enthusiast

I'm trying to Extract Invisible and constant attributes from a block using VS2019 ACAD 2023 and SQL Studio

 

If I have another block with the same attributes but visible and not constant they all work.

 

Here is a sample of my code

 

// 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.Visible;

// 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)
{
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;

// 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);

 

*Moderator edit* How to post your code.

0 Likes
718 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

Firstly, please use "</>" button of the toolbar above the message window to post code so that the code formatting is reserved and the code is easy to read.

 

I think you did not realize Entity.Visible property ( AttributeReference/Definition inherits this property, of course) is not AttributeReference.Definition.Invisible property. If you are after Invisible property, then your code is wrong:

 

bool isVisible = attRef.Visible;

 

It should be

 

bool isVisible = attRef.Invisible;

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

rbh2024
Enthusiast
Enthusiast
      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;
        }
0 Likes
Message 4 of 6

rbh2024
Enthusiast
Enthusiast

That did not work either.  If I copy the block and make the attributes visible then all the blocks work even the original.

 

0 Likes
Message 5 of 6

norman.yuan
Mentor
Mentor

This:

if (blk.AttributeCollection.Count > 0 & blk.Name.Contains("*"))

 

Should be

if (blk.AttributeCollection.Count > 0 && blk.Name.Contains("*"))

?

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 6

ActivistInvestor
Mentor
Mentor

What was originally suggested:

 

bool isVisible = attRef.Invisible;

 

 

Should be:

 

 

bool isVisible = ! attRef.Invisible;

 

 

Note the exclamation point.

0 Likes