using BimWeb.DataConnection;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Drawing;
namespace BimWeb.BimWebService
{
[WebService(Namespace = "SxanBimWebService")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class RevitUpload : System.Web.Services.WebService
{
MSSqlHelper MHelper = new MSSqlHelper();
public string RevitFileUrl = ConfigurationManager.AppSettings["UploadRevits"].ToString();
public string ImageFileUrl = ConfigurationManager.AppSettings["UploadImage"].ToString();
#region 获取表BimWeb_ModelType中的数据,返回datatable
[WebMethod]
public DataTable getCatagories()
{
string sqlC = "select Id,Name,ParentIndexId from BimWeb_ModelType ";
DataTable dtModelType = MHelper.ExecuteDataTable(sqlC, "BimWeb_ModelType");
return dtModelType;
}
#endregion
#region 获取表BimWeb_ModelSet中的数据,返回datatable
[WebMethod]
public DataTable getModelSet()
{
string sqlModelSet = "select Id,Name from BimWeb_ModelSet ";
DataTable dtModelSet = MHelper.ExecuteDataTable(sqlModelSet, "BimWeb_ModelSet");
return dtModelSet;
}
#endregion
#region 上传单个族文件Revit 对应的表: BimWeb_Model
[WebMethod]
public int UploadRevit(string uniquid, string name, byte[] content, int categoryid, int setID, byte[] thumb = null)
{
int backUniqueId = -1;
string FileName = "";
string thumbFile = "";
int BimWeb_ModelID = 0;
if (uniquid != "" && uniquid != null)
{
string sqlModelIsExit = "select * from BimWeb_Model where UniqueId='" + uniquid + "'";
DataTable dtIsExit = MHelper.ExecuteDataTable(sqlModelIsExit, "BimWeb_Model");
if (dtIsExit.Rows.Count > 0)
{
backUniqueId = Convert.ToInt16(dtIsExit.Rows[0]["Id"]);
FileName = Convert.ToString(dtIsExit.Rows[0]["RFCModelFilePath"]);
thumbFile = Convert.ToString(dtIsExit.Rows[0]["D3ModelImagePath"]);
BimWeb_ModelID = Convert.ToInt16(dtIsExit.Rows[0]["Id"]);
if (FileName != "")
{
string NewFile2 = Server.MapPath("~/" + FileName);
if (System.IO.File.Exists(NewFile2)) System.IO.File.Delete(NewFile2);
}
string NewFileName2 = uniquid + ".rfa";
string RfaFile2 = RevitFileUrl + NewFileName2;
string NewFile3 = Server.MapPath("~/" + RevitFileUrl) + NewFileName2;
if (System.IO.File.Exists(NewFile3)) System.IO.File.Delete(NewFile3);
System.IO.File.WriteAllBytes(NewFile3, content);
string NewFilethumbName4 = uniquid + ".jpg";
string ImageFile4 = ImageFileUrl + NewFilethumbName4;
if (thumbFile != "")
{
string NewImageFile2 = Server.MapPath("~/" + thumbFile);
if (System.IO.File.Exists(NewImageFile2)) System.IO.File.Delete(NewImageFile2);
using (System.IO.MemoryStream mem = new MemoryStream(thumb))
{
string NewFileJpg = Server.MapPath("~/" + ImageFileUrl) + NewFilethumbName4;
System.Drawing.Bitmap bmp = System.Drawing.Bitmap.FromStream(mem) as System.Drawing.Bitmap;
bmp.Save(NewFileJpg);
}
}
string UpdateBimWeb_Model = "update BimWeb_Model set Name='" + name + "',RFCModelFilePath='" + RfaFile2 + "',ModelTypeIndexId='" + categoryid + "',D3ModelImagePath='" + ImageFile4 + "',ModelSetIndexId=" + setID + " where UniqueId='" + uniquid + "';";
int backRows = MHelper.ExecuteNonQuery(UpdateBimWeb_Model);
if (backRows != -1)
{
string DelBimWeb_ModelInfo = "delete from BimWeb_ModelInfo where ModelIndexId=" + BimWeb_ModelID;
int delBack = MHelper.ExecuteNonQuery(DelBimWeb_ModelInfo);
if (delBack > 0)
{
}
else
{
}
string DelBimWeb_ModelStandard = "delete from BimWeb_ModelStandard where ModelId="+ BimWeb_ModelID;
int delBackS = MHelper.ExecuteNonQuery(DelBimWeb_ModelStandard);
if (delBackS > 0)
{
}
else
{
}
}
}
else
{
string NewFileName2 = uniquid + ".rfa";
string RfaFile = RevitFileUrl + NewFileName2;
string NewFile2 = Server.MapPath("~/" + RevitFileUrl) + NewFileName2;
if (System.IO.File.Exists(NewFile2)) System.IO.File.Delete(NewFile2);
System.IO.File.WriteAllBytes(NewFile2, content);
string NewFilethumbName2 = uniquid + ".jpg";
string ImageFile = ImageFileUrl + NewFilethumbName2;
if (thumb != null)
{
using (System.IO.MemoryStream mem = new MemoryStream(thumb))
{
string NewFileJpg = Server.MapPath("~/" + ImageFileUrl) + NewFilethumbName2;
System.Drawing.Bitmap bmp = System.Drawing.Bitmap.FromStream(mem) as System.Drawing.Bitmap;
bmp.Save(NewFileJpg);
}
}
string InsertBimWeb_Model = "insert into BimWeb_Model(UniqueId,Name,RFCModelFilePath,ModelTypeIndexId,D3ModelImagePath,ModelSetIndexId) values('" + uniquid + "','" + name + "','" + RfaFile + "'," + categoryid + ",'" + ImageFile + "'," + setID + ");select @@IDENTITY";
backUniqueId = MHelper.ExecuteScalerB(InsertBimWeb_Model);
if (backUniqueId != -1)
{
}
else
{
}
}
}
return backUniqueId;
}
#endregion
#region 上传参数 对应表 BimWeb_ModelInfo
[WebMethod]
public bool UploadRevitParameters(int modelid, string key, string value)
{
string InsertModelInfo = "insert into BimWeb_ModelInfo(ModelIndexId,[Key],Value) values('" + modelid + "','" + key + "','" + value + "')";
int backValue = MHelper.ExecuteNonQuery(InsertModelInfo);
if (backValue > 0)
{
return true;
}
else
{
return false;
}
}
#endregion
#region 上传 BimWeb_ModelStandard
[WebMethod]
public bool UploadRevitTypes(int modelid, string name, byte[] BimWeb_ModelStandard = null)
{
string NewFilethumbName2 = Guid.NewGuid().ToString() + ".jpg";
string ImageFile = ImageFileUrl + NewFilethumbName2;
if (BimWeb_ModelStandard != null)
{
using (System.IO.MemoryStream mem = new MemoryStream(BimWeb_ModelStandard))
{
string NewFileJpg = Server.MapPath("~/" + ImageFileUrl) + NewFilethumbName2;
System.Drawing.Bitmap bmp = System.Drawing.Bitmap.FromStream(mem) as System.Drawing.Bitmap;
bmp.Save(NewFileJpg);
}
}
string InsertModelStandard = "insert into BimWeb_ModelStandard(Name,D3ModelImagePath,ModelId) values('" + name + "','" + ImageFile + "','" + modelid + "')";
if (MHelper.ExecuteNonQuery(InsertModelStandard) > 0)
{
return true;
}
else
{
return false;
}
}
#endregion
}
}