C# 代码在MultiSheetsPdf中给出错误
这是我创建dwg文件PDF的代码,但它在MultiSheetPdf附近给了我错误。请给我同样的解决方案。 我认为链接是问题所在,但我无法确定,请向我建议解决方案C# 代码在MultiSheetsPdf中给出错误,c#,autocad,export-to-pdf,autocad-plugin,C#,Autocad,Export To Pdf,Autocad Plugin,这是我创建dwg文件PDF的代码,但它在MultiSheetPdf附近给了我错误。请给我同样的解决方案。 我认为链接是问题所在,但我无法确定,请向我建议解决方案 namespace Plottings { public class MultiSheetsPdf { private string dwgFile, pdfFile, dsdFile, outputDir; private int sheetNum; private IEnumerable<
namespace Plottings
{
public class MultiSheetsPdf
{
private string dwgFile, pdfFile, dsdFile, outputDir;
private int sheetNum;
private IEnumerable<Layout> layouts;
private const string LOG = "publish.log";
public MultiSheetsPdfPlot(string pdfFile, IEnumerable<Layout> layouts)
{
Database db = HostApplicationServices.WorkingDatabase;
this.dwgFile = db.Filename;
this.pdfFile = pdfFile;
this.outputDir = Path.GetDirectoryName(this.pdfFile);
this.dsdFile = Path.ChangeExtension(this.pdfFile, "dsd");
this.layouts = layouts;
}
public void Publish()
{
if (TryCreateDSD())
{
Publisher publisher = AcAp.Publisher;
PlotProgressDialog plotDlg = new PlotProgressDialog(false, this.sheetNum, true);
publisher.PublishDsd(this.dsdFile, plotDlg);
plotDlg.Destroy();
File.Delete(this.dsdFile);
}
}
private bool TryCreateDSD()
{
using (DsdData dsd = new DsdData())
using (DsdEntryCollection dsdEntries = CreateDsdEntryCollection(this.layouts))
{
if (dsdEntries == null || dsdEntries.Count <= 0) return false;
if (!Directory.Exists(this.outputDir))
Directory.CreateDirectory(this.outputDir);
this.sheetNum = dsdEntries.Count;
dsd.SetDsdEntryCollection(dsdEntries);
dsd.SetUnrecognizedData("PwdProtectPublishedDWF", "FALSE");
dsd.SetUnrecognizedData("PromptForPwd", "FALSE");
dsd.SheetType = SheetType.MultiDwf;
dsd.NoOfCopies = 1;
dsd.DestinationName = this.pdfFile;
dsd.IsHomogeneous = false;
dsd.LogFilePath = Path.Combine(this.outputDir, LOG);
PostProcessDSD(dsd);
return true;
}
}
private DsdEntryCollection CreateDsdEntryCollection(IEnumerable<Layout> layouts)
{
DsdEntryCollection entries = new DsdEntryCollection();
foreach (Layout layout in layouts)
{
DsdEntry dsdEntry = new DsdEntry();
dsdEntry.DwgName = this.dwgFile;
dsdEntry.Layout = layout.LayoutName;
dsdEntry.Title = Path.GetFileNameWithoutExtension(this.dwgFile) + "-" + layout.LayoutName;
dsdEntry.Nps = layout.TabOrder.ToString();
entries.Add(dsdEntry);
}
return entries;
}
private void PostProcessDSD(DsdData dsd)
{
string str, newStr;
string tmpFile = Path.Combine(this.outputDir, "temp.dsd");
dsd.WriteDsd(tmpFile);
using (StreamReader reader = new StreamReader(tmpFile, Encoding.Default))
using (StreamWriter writer = new StreamWriter(this.dsdFile, false, Encoding.Default))
{
while (!reader.EndOfStream)
{
str = reader.ReadLine();
if (str.Contains("Has3DDWF"))
{
newStr = "Has3DDWF=0";
}
else if (str.Contains("OriginalSheetPath"))
{
newStr = "OriginalSheetPath=" + this.dwgFile;
}
else if (str.Contains("Type"))
{
newStr = "Type=6";
}
else if (str.Contains("OUT"))
{
newStr = "OUT=" + this.outputDir;
}
else if (str.Contains("IncludeLayer"))
{
newStr = "IncludeLayer=TRUE";
}
else if (str.Contains("PromptForDwfName"))
{
newStr = "PromptForDwfName=FALSE";
}
else if (str.Contains("LogFilePath"))
{
newStr = "LogFilePath=" + Path.Combine(this.outputDir, LOG);
}
else
{
newStr = str;
}
writer.WriteLine(newStr);
}
}
File.Delete(tmpFile);
}
[CommandMethod("PlotPdf")]
public void PlotPdf()
{
Database db = HostApplicationServices.WorkingDatabase;
short bgp = (short)Application.GetSystemVariable("BACKGROUNDPLOT");
try
{
Application.SetSystemVariable("BACKGROUNDPLOT", 0);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
List<Layout> layouts = new List<Layout>();
DBDictionary layoutDict =
(DBDictionary)db.LayoutDictionaryId.GetObject(OpenMode.ForRead);
foreach (DBDictionaryEntry entry in layoutDict)
{
if (entry.Key != "Model")
{
layouts.Add((Layout)tr.GetObject(entry.Value, OpenMode.ForRead));
}
}
layouts.Sort((l1, l2) => l1.TabOrder.CompareTo(l2.TabOrder));
string filename = Path.ChangeExtension(db.Filename, "pdf");
MultiSheetsPdf plotter = new MultiSheetsPdf(filename, layouts);
plotter.Publish();
tr.Commit();
}
}
catch (System.Exception e)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nError: {0}\n{1}", e.Message, e.StackTrace);
}
finally
{
Application.SetSystemVariable("BACKGROUNDPLOT", bgp);
}
}
}
}
名称空间绘图
{
公共类多页PDF
{
私有字符串dwgFile、pdfFile、dsdFile、outputDir;
私有int sheetNum;
私人可数布局;
private const string LOG=“publish.LOG”;
公共多页PDFPlot(字符串pdfFile、IEnumerable布局)
{
Database db=HostApplicationServices.WorkingDatabase;
this.dwgFile=db.Filename;
this.pdfFile=pdfFile;
this.outputDir=Path.GetDirectoryName(this.pdfFile);
this.dsdFile=Path.ChangeExtension(this.pdfFile,“dsd”);
这个布局=布局;
}
公开发布()
{
if(TryCreateDSD())
{
Publisher-Publisher=AcAp.Publisher;
PlotProgressDialog plotDlg=新建PlotProgressDialog(false,this.sheetNum,true);
publisher.PublishDsd(this.dsd文件,plotDlg);
plotDlg.Destroy();
文件.Delete(此.dsdFile);
}
}
私有bool TryCreateDSD()
{
使用(DsdData dsd=new DsdData())
使用(DsdEntryCollection dsdEntries=CreateDsdEntryCollection(this.layouts))
{
if(dsdEntries==null | | dsdEntries.Count l1.TabOrder.CompareTo(l2.TabOrder));
字符串文件名=Path.ChangeExtension(db.filename,“pdf”);
MultiSheetsPdf绘图仪=新的MultiSheetsPdf(文件名、布局);
plotter.Publish();
tr.Commit();
}
}
捕获(System.e例外)
{
Editor ed=Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(“\n错误:{0}\n{1}”,e.Message,e.StackTrace);
}
最后
{
Application.SetSystemVariable(“背景图”,bgp);
}
}
}
}
给您:(请注意并理解您的版本与此版本之间的差异)
名称空间绘图
{
公共类多页PDF
{
私有字符串dwgFile、pdfFile、dsdFile、outputDir;
私有int sheetNum;
私人可数布局;
private const string LOG=“publish.LOG”;
公共多页PDF(){}
公共多页PDF(字符串PDF、IEnumerable布局)
{
Database db=HostApplicationServices.WorkingDatabase;
this.dwgFile=db.Filename;
this.pdfFile=pdfFile;
this.outputDir=Path.GetDirectoryName(this.pdfFile);
this.dsdFile=Path.ChangeExtension(this.pdfFile,“dsd”);
这个布局=布局;
}
公开发布()
{
if(TryCreateDSD())
{
Publisher-Publisher=AcAp.Publisher;
PlotProgressDialog plotDlg=新建PlotProgressDialog(false,this.sheetNum,true);
publisher.PublishDsd(this.dsd文件,plotDlg);
plotDlg.Destroy();
文件.Delete(此.dsdFile);
}
}
私有bool TryCreateDSD()
{
使用(DsdData dsd=new DsdData())
使用(DsdEntryCollection dsdEntries=CreateDsdEntryCollection(this.layouts))
{
if(dsdEntries==null | | dsdEntries.Count l1.TabOrder.CompareTo(l2.TabOrder));
字符串文件名=Path.ChangeExtension(db.filename,“pdf”);
MultiSheetsPdf绘图仪=新的MultiSheetsPdf(文件名、布局);
plotter.Publish();
tr.Commit();
}
}
捕获(System.e例外)
{
Editor ed=Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(“\n错误:{0}\n{1}”,e.Message,e.StackTrace);
}
最后
{
Application.SetSystemVariable(“背景图”,bgp);
}
}
}
}
抬起头来。这种方法,即后处理DSD,测试太通用了。客户联系了我,抱怨他的一份文件没有密谋。它被命名为“南方”。字符串中“OUT”的测试导致了问题。没有抛出错误。只是一个很好的时尚迷。将所有测试更改为包含“=”。否则,如果(str.Contains(“OUT=){…您能显示您得到了什么错误吗?CS1520方法必须有一个返回类型CS1729'MultiSheetsPdf'不包含一个接受2个参数的构造函数,因为您打算在那里定义一个构造函数。请尝试更改您的公共MultiSheetsPdfPlot(字符串pdfFile,IEnumerable布局)到公共多页PDF(字符串pdfFile,IEnumerable布局)。事实上,我是新来的。请您根据更改编辑我的代码。通常,当您要求他们为您修改代码时,人们不喜欢这样。但是,由于您非常新,请参见下面的答案。您的构造函数定义中有错误。至少,如果您的引用和命名空间为重新更正。仍然在构造函数“public multisheetsfd(){}”处给出错误as Methods必须有返回类型。抱歉,位置错误。将该代码移到现有构造函数的正上方。现在它不应该有任何与构造函数相关的错误。我想问的最后一件事是,如何更改保存pdf文件的位置?这一行:string filename=Path.ChangeExtension(db.filename,“pdf”)所以,将db.Filename设置为要保存的位置。它可能来自数据库,所以最好在那里更改它本身。我在Swamp编写并发布了上面的MultiSheetsPdf类的代码:“CommandMethod”没有在MultiSheetsPdfand中定义,因为它没有什么需要修改的
namespace Plottings
{
public class MultiSheetsPdf
{
private string dwgFile, pdfFile, dsdFile, outputDir;
private int sheetNum;
private IEnumerable<Layout> layouts;
private const string LOG = "publish.log";
public MultiSheetsPdf(){}
public MultiSheetsPdf(string pdfFile, IEnumerable<Layout> layouts)
{
Database db = HostApplicationServices.WorkingDatabase;
this.dwgFile = db.Filename;
this.pdfFile = pdfFile;
this.outputDir = Path.GetDirectoryName(this.pdfFile);
this.dsdFile = Path.ChangeExtension(this.pdfFile, "dsd");
this.layouts = layouts;
}
public void Publish()
{
if (TryCreateDSD())
{
Publisher publisher = AcAp.Publisher;
PlotProgressDialog plotDlg = new PlotProgressDialog(false, this.sheetNum, true);
publisher.PublishDsd(this.dsdFile, plotDlg);
plotDlg.Destroy();
File.Delete(this.dsdFile);
}
}
private bool TryCreateDSD()
{
using (DsdData dsd = new DsdData())
using (DsdEntryCollection dsdEntries = CreateDsdEntryCollection(this.layouts))
{
if (dsdEntries == null || dsdEntries.Count <= 0) return false;
if (!Directory.Exists(this.outputDir))
Directory.CreateDirectory(this.outputDir);
this.sheetNum = dsdEntries.Count;
dsd.SetDsdEntryCollection(dsdEntries);
dsd.SetUnrecognizedData("PwdProtectPublishedDWF", "FALSE");
dsd.SetUnrecognizedData("PromptForPwd", "FALSE");
dsd.SheetType = SheetType.MultiDwf;
dsd.NoOfCopies = 1;
dsd.DestinationName = this.pdfFile;
dsd.IsHomogeneous = false;
dsd.LogFilePath = Path.Combine(this.outputDir, LOG);
PostProcessDSD(dsd);
return true;
}
}
private DsdEntryCollection CreateDsdEntryCollection(IEnumerable<Layout> layouts)
{
DsdEntryCollection entries = new DsdEntryCollection();
foreach (Layout layout in layouts)
{
DsdEntry dsdEntry = new DsdEntry();
dsdEntry.DwgName = this.dwgFile;
dsdEntry.Layout = layout.LayoutName;
dsdEntry.Title = Path.GetFileNameWithoutExtension(this.dwgFile) + "-" + layout.LayoutName;
dsdEntry.Nps = layout.TabOrder.ToString();
entries.Add(dsdEntry);
}
return entries;
}
private void PostProcessDSD(DsdData dsd)
{
string str, newStr;
string tmpFile = Path.Combine(this.outputDir, "temp.dsd");
dsd.WriteDsd(tmpFile);
using (StreamReader reader = new StreamReader(tmpFile, Encoding.Default))
using (StreamWriter writer = new StreamWriter(this.dsdFile, false, Encoding.Default))
{
while (!reader.EndOfStream)
{
str = reader.ReadLine();
if (str.Contains("Has3DDWF"))
{
newStr = "Has3DDWF=0";
}
else if (str.Contains("OriginalSheetPath"))
{
newStr = "OriginalSheetPath=" + this.dwgFile;
}
else if (str.Contains("Type"))
{
newStr = "Type=6";
}
else if (str.Contains("OUT"))
{
newStr = "OUT=" + this.outputDir;
}
else if (str.Contains("IncludeLayer"))
{
newStr = "IncludeLayer=TRUE";
}
else if (str.Contains("PromptForDwfName"))
{
newStr = "PromptForDwfName=FALSE";
}
else if (str.Contains("LogFilePath"))
{
newStr = "LogFilePath=" + Path.Combine(this.outputDir, LOG);
}
else
{
newStr = str;
}
writer.WriteLine(newStr);
}
}
File.Delete(tmpFile);
}
[CommandMethod("PlotPdf")]
public void PlotPdf()
{
Database db = HostApplicationServices.WorkingDatabase;
short bgp = (short)Application.GetSystemVariable("BACKGROUNDPLOT");
try
{
Application.SetSystemVariable("BACKGROUNDPLOT", 0);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
List<Layout> layouts = new List<Layout>();
DBDictionary layoutDict =
(DBDictionary)db.LayoutDictionaryId.GetObject(OpenMode.ForRead);
foreach (DBDictionaryEntry entry in layoutDict)
{
if (entry.Key != "Model")
{
layouts.Add((Layout)tr.GetObject(entry.Value, OpenMode.ForRead));
}
}
layouts.Sort((l1, l2) => l1.TabOrder.CompareTo(l2.TabOrder));
string filename = Path.ChangeExtension(db.Filename, "pdf");
MultiSheetsPdf plotter = new MultiSheetsPdf(filename, layouts);
plotter.Publish();
tr.Commit();
}
}
catch (System.Exception e)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nError: {0}\n{1}", e.Message, e.StackTrace);
}
finally
{
Application.SetSystemVariable("BACKGROUNDPLOT", bgp);
}
}
}
}