.NET

Reply
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 11 of 36 (254 Views)

Re: Repeatedly Insert Block

01-24-2012 01:24 PM in reply to: cincir

Thanks a million, i've got it up and running so far, I really do appreciate everyone's help. I have just one last question that may be a bit unrealted, however here goes. When I create my new block reference and append it, it imports on the layer it was on in it's original DWG. How do i specify it to import onto a layer of my choosing. I've tried this already:

 

for (int i = 0; i < quantity; i++)
{

BlockReference bref = new BlockReference(pCenter, BlkId);

bref.Layer = "CH-SPR";

btr.AppendEntity(bref);

tr.AddNewlyCreatedDBObject(bref, true);

pCenter += vOffset;

bref.ExplodeToOwnerSpace();

bref.Erase();

 

}

}
catch
{
MessageBox.Show("Failed to read the DWG from the refence folder, are you sure it is present?");
}
finally
{
tr.Commit();
this.Close();
}

 

Thanks again

Vince

Active Contributor
cincir
Posts: 32
Registered: ‎08-12-2011
Message 12 of 36 (252 Views)

Re: Repeatedly Insert Block

01-24-2012 01:57 PM in reply to: vince1327

what is wrong with your code. it will work if there is a layer named like that. bu you have to set the attributereferences' layer to that only if they exist.if you want to assign LayerId property of the object you must first get your layertablerecord first like this 

LayerTable acLayerTbl = acTrans.GetObject(acCurDB.LayerTableId,OpenMode.ForRead) as LayerTable;

ObjectId acLayerId = ObjectId.Null;
if (acLayerTbl.Has("SomeLayerName"))
{
    acLayerId = acLayerTbl["SomeLayerName"];
}
else
{
    LayerTableRecord acLayerTblRec = new LayerTableRecord();
    acLayerTblRec.Name = "SomeLayerName";
    acLayerTbl.UpgradeOpen();
    acLayerId = acLayerTbl.Add(acLyerTblRec);
    acTrans.AddNewlyCreatedObject(acLayerTblRec,true);
}

acBlkRef.LayerId = acLayerId;

 use this aproach if you want to check if there is the layer you want to assign and create if not.

 

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 13 of 36 (235 Views)

Re: Repeatedly Insert Block

01-25-2012 05:58 AM in reply to: cincir

Hey Cincir,

 

I've actually create the layer beforehand and can verify that it exists...the code looks like below. I'm not sure why it won't import onto the CH-SPR layer though, this has been one of those small things on my list for weeks now and it's driving me mad.

 

Transaction tr = doc.TransactionManager.StartTransaction();
using (DocumentLock acLckDoc = doc.LockDocument())

try
{

//Check to see if the layer CH-SPR already exists, if so use it, if not create it and then use it
string sLayerName1 = "CH-SPR";

LayerTable acLyrTbl1; acLyrTbl1 = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
LayerTableRecord acLyrTblRec;

if (acLyrTbl1.Has(sLayerName1) == false)
{
acLyrTblRec = new LayerTableRecord();
// Assign the layer a name
acLyrTblRec.Name = sLayerName1;

// Upgrade the Layer table for write
if (acLyrTbl1.IsWriteEnabled == false) acLyrTbl1.UpgradeOpen();

// Append the new layer to the Layer table and the transaction
acLyrTbl1.Add(acLyrTblRec);
tr.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
else
{
// Open the layer if it already exists for write
acLyrTblRec = tr.GetObject(acLyrTbl1[sLayerName1], OpenMode.ForWrite) as LayerTableRecord;

}


//This function will import the selected dwg into modelspace and activate it
if (comboBox1.Text == "FACSPR7")
{
comp = "C:\\ACADSTUFF\\FACSPR7.dwg";

}
if (comboBox1.Text == "FACSPR1")
{
comp = "C:\\ACADSTUFF\\FACSPR1.dwg";

}
if (comboBox1.Text == "FACSPR17")
{
comp = "C:\\ACADSTUFF\\FACSPR17.dwg";

}
if (comboBox1.Text == "FACSPR22")
{
comp = "C:\\ACADSTUFF\\FACSPR22.dwg";

}
if (comboBox1.Text == "FACSPR31")
{
comp = "C:\\ACADSTUFF\\FACSPR31.dwg";

}

string dwgName = HostApplicationServices.Current.FindFile(comp, acadApp.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default);

Database db1 = new Database(false, false);

db1.ReadDwgFile(dwgName, System.IO.FileShare.Read, true, "");

ObjectId BlkId;

BlkId = doc.Database.Insert(dwgName, db1, false);

BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, true);

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

int quantity = Convert.ToInt32(numericUpDown1.Value);
Point3d pCenter = new Point3d(0, 0, 0);
Vector3d vOffset = new Vector3d(1000, 0, 0);

for (int i = 0; i < quantity; i++)
{

BlockReference bref = new BlockReference(pCenter, BlkId);

bref.Layer = "CH-SPR";

btr.AppendEntity(bref);

tr.AddNewlyCreatedDBObject(bref, true);

pCenter += vOffset;

bref.ExplodeToOwnerSpace();

bref.Erase();

 

}

}
catch
{
MessageBox.Show("Failed to read the DWG from the refence folder, are you sure it is present?");
}
finally
{
tr.Commit();
this.Close();
}

 


}

}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}
}

}

Active Contributor
cincir
Posts: 32
Registered: ‎08-12-2011
Message 14 of 36 (233 Views)

Re: Repeatedly Insert Block

01-25-2012 06:18 AM in reply to: vince1327

vince1327 you are assigning layer to the blockref then erase it after exploding so the individual entities' layers that comprise the block definition must be set in order to accomplish this task. Do you have to explode them?

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 15 of 36 (230 Views)

Re: Repeatedly Insert Block

01-25-2012 06:33 AM in reply to: cincir

Hey Cincir,

 

The reason that i'm exploding them to ownerspace is because that is the only way that I can get them to show up on the actual drawing. Is there another way?

 

Cheers

Active Contributor
cincir
Posts: 32
Registered: ‎08-12-2011
Message 16 of 36 (226 Views)

Re: Repeatedly Insert Block

01-25-2012 07:02 AM in reply to: vince1327

comment the explodetomodelspce and erase lines and see what will you get :smileyhappy: 

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 17 of 36 (223 Views)

Re: Repeatedly Insert Block

01-25-2012 07:22 AM in reply to: cincir

Cool, so that brough it in as a block reference on the correct layer. The problem though is that I need all the attributes that it would normally have if it was exploded...So right now i'm stuck at crossroads...I can have it brought in and explode it, but it's in the wrong layer...or I could bring it into the correct layer as a block reference but not have any of my attributes...is there a middle ground somewhere. Can i explode it and then transfer it to the correct layer?

 

 

Thanks so much!

Active Contributor
cincir
Posts: 32
Registered: ‎08-12-2011
Message 18 of 36 (220 Views)

Re: Repeatedly Insert Block

01-25-2012 07:39 AM in reply to: vince1327

you are at the wrong tides. your blockreference can have all the attributedefinitions as your blockdefinition as attributereference. :smileywink:

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 19 of 36 (218 Views)

Re: Repeatedly Insert Block

01-25-2012 07:42 AM in reply to: cincir

hey cincir, i'm not sure what you mean by this...sorry i'm actually very new to this and sometimes the terminology along with the api are very confusing. How do i implement the attributereference that you're talking about?

Active Contributor
cincir
Posts: 32
Registered: ‎08-12-2011
Message 20 of 36 (213 Views)

Re: Repeatedly Insert Block

01-25-2012 08:09 AM in reply to: vince1327
BlkId = doc.Database.Insert(dwgName, db1, false);
BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, true);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

int quantity = Convert.ToInt32(numericUpDown1.Value);
Point3d pCenter = new Point3d(0, 0, 0);
Vector3d vOffset = new Vector3d(1000, 0, 0);

BlockTableRecord acBlkDef = acTrans.GetObject(BlkId,OpenMode.ForRead) as BlockTableRecord;

for (int i = 0; i < quantity; i++)
{
   BlockReference bref = new BlockReference(pCenter, BlkId);
   
   if (acBlkDef.HasAttributeDefinitions)
   {
      foreach(ObjectId acAttId in acBlkDef)
      {
         Entity acTempEnt = (Entity)acTrans.GetObject(acAttId,OpenMode.ForRead);
         if (acTempEnt is AttributeDefinition)
         {
             AttributeReference acAttRef = new AttributeReference();
              acAttRef.SetAttributeFromBlock((AttributeDefinition)acTempEnt,bref.BlockTransform);
             acAttRef.Layer = "CH-SPR";
             bref.AttributeCollection.AppendAttribute(acAttRef);
             acTrans.AddNewlyCreatedDBObject(acAttRef,true);  
         }
         
      }
   }
   
   bref.Layer = "CH-SPR";
   btr.AppendEntity(bref);
   tr.AddNewlyCreatedDBObject(bref, true);
   pCenter += vOffset;
}

 it is hard to write code without Visual Studio Intellisence :smileyhappy:

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community