- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi every one
Im having trouble inserting a block from another dwg file.
I have a form which contains a checked listbox which when ever the user single clicks on a block name from the checked list box, the program reads a preview image of that clicked block from a specific file location. my first question: is it possible to some how attach the images on the file itself so there would be no need to read images from an external file? if so, can any one tell me how to do that?
my second question is, I was searching through Kean's website which I found an application where he inserts a block from another drawing into the current one, which the code is down below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
namespace valve_blocks
{
public class Util
{
[CommandMethod("NSVVALVES")]
public void valves()
{
valve frm_valves = new valve();
frm_valves.Show();
}
public static void importBlock(string blkname)
{
DocumentCollection dm = Application.DocumentManager;
Editor ed = dm.MdiActiveDocument.Editor;
Database destDb = dm.MdiActiveDocument.Database;
Database sourceDb = new Database(false, true);
dm.MdiActiveDocument.LockDocument();
try
{
// Get name of DWG from which to copy blocks
//sourceFileName =ed.GetString("D:\C# codes\Block Insertion\legend-rev01.dwg");
string address = "C:/NSV/Block Icons/Blocks.dwg";
// Read the DWG into a side database
sourceDb.ReadDwgFile(address, System.IO.FileShare.Read, true, "");
// Create a variable to store the list of block identifiers
ObjectIdCollection blockIds = new ObjectIdCollection();
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = sourceDb.TransactionManager;
using (Transaction myT = tm.StartTransaction())
{
// Open the block table
BlockTable bt = (BlockTable)tm.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);
// Check each block in the block table
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = (BlockTableRecord)tm.GetObject(btrId, OpenMode.ForRead, false);
// Only add named & non-layout blocks to the copy list
if (!btr.IsAnonymous && !btr.IsLayout && btr.Name=="blkname") blockIds.Add(btrId);
btr.Dispose();
}
}
// Copy blocks from source to destination database
IdMapping mapping = new IdMapping();
sourceDb.WblockCloneObjects(blockIds, destDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
//ed.WriteMessage("\nCopied " + blockIds.Count.ToString() + " block definitions from " + sourceFileName.StringResult + " to the current drawing.");
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\nError during copy: " + ex.Message);
}
sourceDb.Dispose();
}
}
}
that part works fine by itself, but when I call this method from a button which passes the checked items from the checked list box, it wont insert the blocks in the drawing. it doesnt even throw an error or exception. does it have to do some thing with the documentlock? which part am i doing worng?
this is my form code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace valve_blocks
{
public partial class valve : Form
{
public valve()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void valve_Load(object sender, EventArgs e)
{
lst_valves.Items.Add("Backflow Preventer-double check type");
lst_valves.Items.Add("Backflow Preventer-reduced pressure zone");
lst_valves.Items.Add("butterfly");
lst_valves.Items.Add("Cap");
lst_valves.Items.Add("Check Valv");
}
private void lst_valves_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void lst_valves_MouseClick(object sender, MouseEventArgs e)
{
try
{
switch(lst_valves.SelectedItem)
{
case ("Backflow Preventer-double check type"):
PicBox.Image=Image.FromFile("C:/NSV/Block icons/Backflow Preventer-double check type.bmp");
break;
case ("Backflow Preventer-reduced pressure zone"):
PicBox.Image=Image.FromFile("C:/NSV/Block icons/Backflow Preventer-reduced pressure zone.bmp");
break;
case ("butterfly"):
PicBox.Image=Image.FromFile("C:/NSV/Block icons/butterfly.bmp");
break;
case ("Cap"):
PicBox.Image=Image.FromFile("C:/NSV/Block icons/Cap.bmp");
break;
case ("Check Valv"):
PicBox.Image=Image.FromFile("C:/NSV/Block icons/Check Valv.bmp");
break;
}
}
catch (Exception ex)
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("NSV ERROR: "+ ex.Message);
}
}
//button event which does not work
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
foreach (string blkname in lst_valves.CheckedItems)
{
Util.importBlock(blkname);
}
return;
}
}
}
and this is how the form looks:
Solved! Go to Solution.