got a chance to play a little bit on this, see the below code in c#, since i do not do anything in vb.net, it works here, did a lot of refactoring and also in some code spots you are passing a double instead of a string on your locations like this: myDynamProp.GetAllowedValues(); before adding the values into your results dictionary.
public class DynamicBlockHelper
{
#region "Block Insertion Methods"
public static void AddDynBlockToDrawingFromFile(string blockFilePath, string blockName, bool forceInsert = false)
{
var doc = Application.DocumentManager.MdiActiveDocument;
var destDb = doc.Database;
try
{
var blockDefFound = true;
if (!forceInsert)
{
using (var transaction = destDb.TransactionManager.StartTransaction())
{
var myBlockTable = (BlockTable)transaction.GetObject(destDb.BlockTableId, OpenMode.ForRead);
if (!myBlockTable.Has(blockName))
{
blockDefFound = false;
}
transaction.Commit();
}
}
if (blockDefFound) return;
using (var sourceDb = new Database(false, true))
{
sourceDb.ReadDwgFile(blockFilePath, FileShare.Read, true, "");
//sourceDb.CloseInput(true);
destDb.Insert(blockName, sourceDb, false);
}
}
catch (Exception ex)
{
}
}
public static ObjectId InsertBlock(Database databaseIn, string btrToAddTo, Point3d insPt, string blockName,
double xScale, double yScale, double zScale, string layerName, double rotation = 0)
{
if (string.IsNullOrEmpty(layerName)) return ObjectId.Null;
var id = ObjectId.Null;
using (var transaction = databaseIn.TransactionManager.StartTransaction())
{
try
{
var blockTbl = (BlockTable) transaction.GetObject(databaseIn.BlockTableId, OpenMode.ForRead);
if (blockTbl.Has(blockName) && blockTbl.Has(btrToAddTo))
{
var blockDef =
(BlockTableRecord) transaction.GetObject(blockTbl[blockName], OpenMode.ForRead);
var blockTblRecord =
(BlockTableRecord) transaction.GetObject(blockTbl[btrToAddTo], OpenMode.ForWrite);
var blockRef = new BlockReference(insPt, blockDef.Id)
{
ScaleFactors = new Scale3d(xScale, yScale, zScale),
Rotation = rotation
};
blockTblRecord.AppendEntity(blockRef);
transaction.AddNewlyCreatedDBObject(blockRef, true);
var blockEnt = (Entity) transaction.GetObject(blockRef.Id, OpenMode.ForWrite);
try
{
blockEnt.Layer = layerName;
}
catch (Exception ex)
{
}
var attribs = blockRef.AttributeCollection;
foreach (var entObjectId in blockDef)
{
var entity =
(Entity) transaction.GetObject(entObjectId, OpenMode.ForWrite);
if (!(entity is AttributeDefinition)) continue;
var attribDef = entity as AttributeDefinition;
using (var attributeReference = new AttributeReference())
{
attributeReference.SetAttributeFromBlock(attribDef, blockRef.BlockTransform);
attribs.AppendAttribute(attributeReference);
transaction.AddNewlyCreatedDBObject(attributeReference, true);
}
}
transaction.Commit();
id = blockRef.Id;
}
}
catch (Exception ex)
{
transaction.Abort();
}
}
return id;
}
#endregion
#region "Get / Set Block Properties"
public static Dictionary<string, List<string>> GetBlockProperties(ObjectId blockId)
{
var results = new Dictionary<string, List<string>>();
using (var transaction = blockId.Database.TransactionManager.StartTransaction())
{
var dbObject = transaction.GetObject(blockId, OpenMode.ForRead);
if (dbObject is BlockTableRecord)
{
var blockTableRecord = dbObject as BlockTableRecord;
var blockReference =
(BlockReference)
blockTableRecord.GetBlockReferenceIds(false, false)[0].GetObject(OpenMode.ForRead);
foreach (DynamicBlockReferenceProperty myDynamProp in
blockReference.DynamicBlockReferencePropertyCollection)
{
var propKey = string.Format("{0}~{1}~{2}~{3}", myDynamProp.PropertyName, myDynamProp.Description,
myDynamProp.PropertyTypeCode, myDynamProp.UnitsType);
if (results.ContainsKey(propKey)) continue;
AddPropertyKeyAndValues(myDynamProp, results, propKey);
}
}
else
{
var tempBlockRef = dbObject as BlockReference;
if (tempBlockRef != null)
{
foreach (DynamicBlockReferenceProperty myDynamProp in
tempBlockRef.DynamicBlockReferencePropertyCollection)
{
var propKey = string.Format("{0}~{1}~{2}~{3}", myDynamProp.PropertyName,
myDynamProp.Description, myDynamProp.PropertyTypeCode, myDynamProp.UnitsType);
if (results.ContainsKey(propKey)) continue;
AddPropertyKeyAndValues(myDynamProp, results, propKey);
}
}
}
transaction.Commit();
}
return results;
}
private static void AddPropertyKeyAndValues(DynamicBlockReferenceProperty myDynamProp, Dictionary<string, List<string>> results,
string propKey)
{
var vals = new List<string>();
var getAllowedValues = myDynamProp.GetAllowedValues();
foreach (var v in getAllowedValues)
{
if (!(v is string)) continue;
var s = v as string;
if (!vals.Contains(s))
{
vals.Add(s);
}
}
results.Add(propKey, vals);
}
public static bool SetParameter(ObjectId blockId, string parameterName, string value)
{
using (var transaction = blockId.Database.TransactionManager.StartTransaction())
{
try
{
var blockReference = (BlockReference)transaction.GetObject(blockId, OpenMode.ForWrite);
foreach (DynamicBlockReferenceProperty property in blockReference.DynamicBlockReferencePropertyCollection)
{
if (property.ReadOnly || !property.PropertyName.Equals(parameterName, StringComparison.CurrentCultureIgnoreCase))
continue;
switch (property.PropertyTypeCode)
{
case 5:
property.Value = value;
break;
}
transaction.Commit();
return true;
}
}
catch (Exception ex)
{
transaction.Abort();
}
return false;
}
}
#endregion
} [CommandMethod("RunAutomatedMemoryLeakTest")]
public static void AutoMemoryLeakTestCommand()
{
var blockObjectIDs = new List<ObjectId>();
var directoryPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
if (string.IsNullOrWhiteSpace(directoryPath)) return;
foreach (var filePathLoopVariable in Directory.GetFiles(directoryPath))
{
var filePath = filePathLoopVariable;
if (string.IsNullOrWhiteSpace(filePath)) continue;
if (!Path.GetExtension(filePath).Equals(".dwg", StringComparison.CurrentCultureIgnoreCase)) continue;
DynamicBlockHelper.AddDynBlockToDrawingFromFile(filePath,
Path.GetFileNameWithoutExtension(filePath));
blockObjectIDs.Add(DynamicBlockHelper.InsertBlock(HostApplicationServices.WorkingDatabase,
BlockTableRecord.ModelSpace, new Point3d(0, 0, 0),
Path.GetFileNameWithoutExtension(filePath), 1,
1, 1, "NONE"));
}
foreach (var objIdLoopVariable in blockObjectIDs)
{
var objId = objIdLoopVariable;
//dynamic blockProperties = DynamicBlockHelper.GetBlockProperties(objId);
var blockProperties = DynamicBlockHelper.GetBlockProperties(objId);
foreach (var blockPropLoopVariable in blockProperties)
{
var blockProp = blockPropLoopVariable;
var propertyAttribs = blockProp.Key.Split(Convert.ToChar("~"));
var propertyName = propertyAttribs[0];
var propertyType = propertyAttribs[2];
if (propertyType == null || (propertyType != "5" || blockProp.Value.Count <= 1)) continue;
foreach (var propValue in blockProp.Value)
{
DynamicBlockHelper.SetParameter(objId, propertyName, propValue);
}
}
}
}HTH.-