using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Autodesk;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using System.Reflection;
using System.IO;
namespace RealDWGService
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
class Host : HostApplicationServices
{
public Host()
{
RuntimeSystem.Initialize(this, 1033);
}
public override ModelerFlavor ModelerFlavor { get { return ModelerFlavor.Full; } }
private string SearchPath(string fileName)
{
// check if the file is already with full path
if (System.IO.File.Exists(fileName))
return fileName;
// check application folder
string applicationPath =
Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location
);
if (File.Exists(applicationPath + "\\" + fileName))
return applicationPath + "\\" + fileName;
// search folders in %PATH%
string[] paths =
Environment.GetEnvironmentVariable(
"Path").Split(new char[] { ';' } );
foreach (string path in paths)
{
// some folders end with \\, some don't
string validatedPath = "C:\\Program Files (x86)\\Autodesk\\RealDWG 2013\\" + fileName;
//= Path.GetFullPath(path + "\\" + fileName);
if (File.Exists(validatedPath))
return validatedPath;
}
// check the Fonts folders
string systemFonts =
Environment.GetEnvironmentVariable(
"SystemRoot"
) + "\\Fonts\\";
if (File.Exists(systemFonts + fileName))
return systemFonts + fileName;
string rdwgFonts =
"C:\\Program Files\\Autodesk RealDWG 2007\\Fonts\\";
if (File.Exists(rdwgFonts + fileName))
return rdwgFonts + fileName;
return "";
}
public override string FindFile(
string fileName,
Database database,
FindFileHint hint
)
{
// add extension if needed
if (!fileName.Contains("."))
{
string extension;
switch (hint)
{
case FindFileHint.CompiledShapeFile:
extension = ".shx";
break;
case FindFileHint.TrueTypeFontFile:
extension = ".ttf";
break;
case FindFileHint.PatternFile:
extension = ".pat";
break;
case FindFileHint.ArxApplication:
extension = ".dbx";
break;
case FindFileHint.FontMapFile:
extension = ".fmp";
break;
case FindFileHint.XRefDrawing:
extension = ".dwg";
break;
// Fall through. These could have
// various extensions
case FindFileHint.FontFile:
case FindFileHint.EmbeddedImageFile:
default:
extension = "";
break;
}
fileName += extension;
}
return SearchPath(fileName);
}
}
public static void Draw(string outputFile)
{
string acadTemplateFile = @"C:\hotfiles\CAD_Templates\Common\acad.dwt";
using (Host host
= new Host
()) {
using (Autodesk
.AutoCAD.DatabaseServices.Database db
= new Autodesk
.AutoCAD.DatabaseServices.Database(false,
true)) {
File.Copy(acadTemplateFile, outputFile, false);
db.ReadDwgFile(outputFile, System.IO.FileShare.ReadWrite, false, null);
HostApplicationServices.WorkingDatabase = db;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord iModelSpace = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
ObjectId blockId = ObjectId.Null;
using (Transaction tran = db.TransactionManager.StartTransaction())
{
using (Autodesk
.AutoCAD.DatabaseServices.Database sdb
= new Autodesk
.AutoCAD.DatabaseServices.Database(false,
true)) {
string file = @"C:\hotfiles\CAD_Templates\Ductwork\3D\Elbow 45\DW1645ASY.dwg";
sdb.ReadDwgFile(file, System.IO.FileShare.ReadWrite, false, "");
blockId = db.Insert(file, sdb, true);
}
HostApplicationServices.WorkingDatabase = db;
tran.Commit();
}
BlockReference blockRef
= new BlockReference
(Point3d
.Origin, blockId
); iModelSpace.AppendEntity(blockRef);
tr.AddNewlyCreatedDBObject(blockRef, true);
tr.Commit();
}
db.SaveAs(outputFile, DwgVersion.AC1015);
}
}
}
[WebMethod]
public string DrawTest()
{
string file1 = @"C:\hotfiles\CADTemp\firstfile.dwg";
string file2 = @"C:\hotfiles\CADTemp\seconfile.dwg";
string file3 = @"C:\hotfiles\CADTemp\thridfile.dwg";
if (File.Exists(file1))
{
File.Delete(file1);
}
if (File.Exists(file2))
{
File.Delete(file2);
}
if (File.Exists(file3))
{
File.Delete(file3);
}
try
{
Draw(file1);
Draw(file2);
Draw(file3);
}
catch
{
}
return "Any luck?";
}
}
}