#pragma once
#ifndef PRINTOOLS_H
#define PRINTOOLS_H
namespace PrintTools
{
struct Page
{
CString PageName;
double minx;
double miny;
double maxx;
double maxy;
};
inline AcPlDSDData createDSD(AcPlDSDEntries entries,
CString projectPath,
CString destinationPath,
CString promptForFile)
{
AcPlDSDData dsdDataObj;
CString full_file_path_name = projectPath;
CString dsd = projectPath;
dsd.Replace(_T(".dwg"), _T(".dsd"));
CString path = full_file_path_name;
path.Truncate(path.ReverseFind('\\'));
CString file = full_file_path_name;
file = file.Mid(file.ReverseFind('\\') + 1);
file.Replace(_T(".dwg"), _T(""));
WORD wBOM = 0xFEFF;
DWORD NumberOfBytesWritten;
HANDLE hFile = ::CreateFile(dsd, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); //for UTF16 little endian
::WriteFile(hFile, &wBOM, sizeof(WORD), &NumberOfBytesWritten, NULL);
::CloseHandle(hFile);
WritePrivateProfileString(_T("DWF6Version"), _T("Ver"), _T("1"), dsd);
WritePrivateProfileString(_T("DWF6MinorVersion"), _T("MinorVer"), _T("1"), dsd);
for (int i = 0; i < entries.length(); i++)
{
WritePrivateProfileString(_T("DWF6Sheet:") + file + _T("-") + entries[i].title(), _T("DWG"), full_file_path_name, dsd);
WritePrivateProfileString(_T("DWF6Sheet:") + file + _T("-") + entries[i].title(), _T("Layout"), entries[i].layout(), dsd);
WritePrivateProfileString(_T("DWF6Sheet:") + file + _T("-") + entries[i].title(), _T("Setup"), entries[i].NPS(), dsd);
WritePrivateProfileString(_T("DWF6Sheet:") + file + _T("-") + entries[i].title(), _T("OriginalSheetPath"), full_file_path_name, dsd);
WritePrivateProfileString(_T("DWF6Sheet:") + file + _T("-") + entries[i].title(), _T("Has Plot Port"), _T("0"), dsd);
}
WritePrivateProfileString(_T("Target"), _T("Type"), _T("6"), dsd);
WritePrivateProfileString(_T("Target"), _T("DWF"), destinationPath, dsd);
WritePrivateProfileString(_T("Target"), _T("OUT"), path, dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("IsSheetSet"), _T("FALSE"), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("IsHomogeneous"), _T("FALSE"), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("SheetSet Name"), _T(""), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("NoOfCopies"), _T("1"), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("PlotStampOn"), promptForFile, dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("JobID"), _T("0"), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("SelectionSetName"), _T(""), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("CategoryName"), _T(""), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("LogFilePath"), _T(""), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("IncludeLayer"), _T("FALSE"), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("PromptForDwfName"), _T("FALSE"), dsd);
WritePrivateProfileString(_T("SheetSet Properties"), _T("GenerateDwfName"), _T("FALSE"), dsd);
WritePrivateProfileString(_T("PdfOptions"), _T("ConvertTextToGeometry"), _T("FALSE"), dsd);
WritePrivateProfileString(_T("PdfOptions"), _T("LineMerge"), _T("FALSE"), dsd);
WritePrivateProfileString(_T("PdfOptions"), _T("EmbedTtf"), _T("TRUE"), dsd);
WritePrivateProfileString(_T("PdfOptions"), _T("ImageAntiAliasing"), _T("TRUE"), dsd);
WritePrivateProfileString(_T("PdfOptions"), _T("JPEGImageCompression"), _T("TRUE"), dsd);
dsdDataObj.readDSD(dsd);
DeleteFile(dsd);
return dsdDataObj;
}
inline void PublishViews2MultiSheet(std::vector<Page> pages, bool promptForFile = false)
{
Acad::ErrorStatus es = Acad::eOk;
AcDbDatabase* pDb = acdbCurDwg();
// Create page setup based on the views
AcDbLayoutManager* layoutManager = acdbHostApplicationServices()->layoutManager();
CString activeLayout = layoutManager->findActiveLayout(true);
AcDbObjectId layoutId = layoutManager->findLayoutNamed(activeLayout);
AcDbObjectPointer<AcDbLayout> layout(layoutId, AcDb::OpenMode::kForWrite);
CString deviceName = _T("DWG To PDF.pc3");
TCHAR* layoutName_t = NULL;
es = layout->getLayoutName(layoutName_t);
CString layoutName(layoutName_t);
delete layoutName_t;
for(int i = 0 ; i < pages.size(); i ++)
{
AcDbPlotSettings* plotSettings = new AcDbPlotSettings(layout->modelType());
es = plotSettings->copyFrom(layout);
AcDbPlotSettingsValidator* pPSV = NULL;
pPSV = acdbHostApplicationServices()->plotSettingsValidator();
es = pPSV->setPlotCfgName(plotSettings, deviceName);
pPSV->refreshLists(plotSettings);
es = pPSV->setPlotType(plotSettings, PlotType::kWindow);
es = pPSV->setUseStandardScale(plotSettings, true);
es = pPSV->setStdScaleType(plotSettings, StdScaleType::kScaleToFit);
es = pPSV->setPlotCentered(plotSettings, true);
es = pPSV->setPlotRotation(plotSettings, PlotRotation::k0degrees);
es = pPSV->setPlotPaperUnits(plotSettings, AcDbPlotSettings::kMillimeters);
plotSettings->setPrintLineweights(Adesk::kTrue);
es = pPSV->setPlotViewName(plotSettings, pages[i].PageName);
AcGePoint2d min_pt(min(pages[i].minx, pages[i].maxx), min(pages[i].miny, pages[i].maxy));
AcGePoint2d max_pt(max(pages[i].minx, pages[i].maxx), max(pages[i].miny, pages[i].maxy));
//rotate portrait/landscape
if(max_pt.x - min_pt.x < max_pt.y - min_pt.y)
pPSV->setPlotRotation(plotSettings, PlotRotation::k90degrees);
resbuf fromrb, torb;
fromrb.restype = RTSHORT;
fromrb.resval.rint = 0; // WCS
torb.restype = RTSHORT;
torb.resval.rint = 2; // DCS
acedTrans(asDblArray(min_pt), &fromrb, &torb, FALSE, asDblArray(min_pt));
acedTrans(asDblArray(max_pt), &fromrb, &torb, FALSE, asDblArray(max_pt));
es = pPSV->setPlotWindowArea(plotSettings, min_pt.x, min_pt.y, max_pt.x, max_pt.y);
es = plotSettings->setPlotSettingsName(pages[i].PageName + _T("PS"));
es = plotSettings->addToPlotSettingsDict(pDb);
pPSV->refreshLists(plotSettings);
es = layout->copyFrom(plotSettings);
es = plotSettings->close();
}
layout->close();
//put the plot in foreground
resbuf bp_old = { 0,0 }, bp_0 = { 0,0 };
acedGetVar(_T("BACKGROUNDPLOT"), &bp_old);
bp_0 = bp_old; bp_0.resval.rint = 0;
acedSetVar(_T("BACKGROUNDPLOT"), &bp_0);
CString dwgFileName = curDoc()->fileName();
CString dwgPath = dwgFileName.Mid(0, dwgFileName.ReverseFind(_T('\\')) + 1);
CString output = dwgFileName; output.Replace(_T(".dwg"), _T(".pdf"));
AcPlDSDEntries collection;
for (int i = 0; i < pages.size(); i++)
{
AcPlDSDEntry entry;
entry.setDwgName(dwgFileName);
entry.setLayout(layoutName);
entry.setTitle(pages[i].PageName);
entry.setNPSSourceDWG(entry.dwgName());
entry.setNPS(pages[i].PageName + _T("PS"));
collection.append(entry);
}
AcPlDSDData dsdData = createDSD(collection, dwgFileName, output, promptForFile ? _T("TRUE") : _T("FALSE"));
AcPlPlotConfig* plotConfig = NULL;
acplPlotConfigManager->setCurrentConfig(plotConfig, deviceName);
//acedArxLoad(L"AcPublish.arx"); if not loaded yet
// Publish it
acplPublishExecute(dsdData, plotConfig, true);
//reset the background plot value
acedSetVar(_T("BACKGROUNDPLOT"), &bp_old);
//cleanup
AcDbDictionary* pPlotSetDict = NULL;
es = pDb->getPlotSettingsDictionary(pPlotSetDict, AcDb::kForWrite);
if (es == Acad::eOk)
{
for (int i = 0 ; i < collection.length(); i++)
{
CString NPS = collection[i].NPS();
pPlotSetDict->remove(NPS);
}
pPlotSetDict->close();
}
if (MessageBox(NULL, _T("Открыть PDF?"), _T("Информация"), MB_YESNO) == IDYES)
ShellExecute(NULL, NULL, output, NULL, NULL, SW_SHOW);
}
}
#endif