private MaterialBuilder ExportAppearanceCached(Asset appearance) { if (!exportMaterials || appearance == null) { return(defaultMaterial); } var appearanceName = appearance.Name; if (materialCache.ContainsKey(appearanceName)) { return(materialCache[appearanceName]); } var colorVector = Vector4.One; try { Color tempColor = AttemptGetColor(appearance); colorVector = new Vector4(tempColor.Red / 255f, tempColor.Green / 255f, tempColor.Blue / 255f, (float)tempColor.Opacity); } catch { } var isMetal = IsMetal(appearance); var metallicFactor = isMetal ? 1f : 0f; var material = new MaterialBuilder() .WithMetallicRoughnessShader() .WithChannelParam("BaseColor", colorVector) .WithChannelParam("MetallicRoughness", new Vector4(metallicFactor, 1, 0, 0)); // btw metallic isn't getting applied for some reason // TODO: metallic and roughness materialCache[appearanceName] = material; return(material); }
//get bitmap with force background private Bitmap getBmpByForceBGColor(System.Drawing.Color forceColor) { Bitmap tempgetBmpByForceBGColor = null; if (m_inventorApplication.ActiveDocument == null) { return(null); } Document oDoc = m_inventorApplication.ActiveDocument; //in drawing document, change the sheet color and snapshot if ((oDoc) is DrawingDocument) { //change back color and snapshot, and set the color to the original value DrawingDocument oDrawDoc = (DrawingDocument)oDoc; Inventor.Color oSheetOldColor = oDrawDoc.SheetSettings.SheetColor; Inventor.Color oInvForceColor = m_inventorApplication.TransientObjects.CreateColor(forceColor.R, forceColor.G, forceColor.B); //change color and snapshot the bitmap oDrawDoc.SheetSettings.SheetColor = oInvForceColor; m_inventorApplication.ActiveView.Update(); tempgetBmpByForceBGColor = createRawBitmap(oSize, oCornerPt); //set color back oDrawDoc.SheetSettings.SheetColor = oSheetOldColor; } else //other type of document, set color info of color skema { Inventor.BackgroundTypeEnum oOldBGType = m_inventorApplication.ColorSchemes.BackgroundType; string oOldBGImage = ""; if (oOldBGType == BackgroundTypeEnum.kImageBackgroundType) { oOldBGImage = m_inventorApplication.ActiveColorScheme.ImageFullFileName; } //change background color m_inventorApplication.ColorSchemes.BackgroundType = BackgroundTypeEnum.kImageBackgroundType; if (forceColor == System.Drawing.Color.White) { m_inventorApplication.ActiveColorScheme.ImageFullFileName = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\Resources\\white.bmp"; } m_inventorApplication.ActiveView.Update(); tempgetBmpByForceBGColor = createRawBitmap(oSize, oCornerPt); //set the color back m_inventorApplication.ColorSchemes.BackgroundType = oOldBGType; if (oOldBGType == BackgroundTypeEnum.kImageBackgroundType) { m_inventorApplication.ActiveColorScheme.ImageFullFileName = oOldBGImage; } } return(tempgetBmpByForceBGColor); }
private void m_BrowserEvents_OnBrowserNodeGetDisplayObjects(object BrowserNodeDefinition, ref Inventor.ObjectCollection Objects, Inventor.NameValueMap Context, ref Inventor.HandlingCodeEnum HandlingCode) { PartDocument oPartDocument = m_inventorApplication.ActiveDocument as PartDocument; PartComponentDefinition oPartDef = oPartDocument.ComponentDefinition; if (oHighlight == null) { oHighlight = oPartDocument.CreateHighlightSet(); } else { oHighlight.Clear(); } Inventor.Color oColor = default(Inventor.Color); oColor = m_inventorApplication.TransientObjects.CreateColor(128, 22, 22); // Set the opacity oColor.Opacity = 0.8; oHighlight.Color = oColor; if (BrowserNodeDefinition is ClientBrowserNodeDefinition) { ClientBrowserNodeDefinition oClientB = (ClientBrowserNodeDefinition)BrowserNodeDefinition; //highlight all ExtrudeFeature if (oClientB.Label == "Node2") { foreach (ExtrudeFeature oExtrudeF in oPartDef.Features.ExtrudeFeatures) { oHighlight.AddItem(oExtrudeF); } //highlight all RevolveFeature } else if (oClientB.Label == "Node3") { foreach (RevolveFeature oRevolveF in oPartDef.Features.RevolveFeatures) { oHighlight.AddItem(oRevolveF); } } } }
public void Run(Document doc) { try { LogTrace("Processing " + doc.FullFileName); dynamic invDoc = doc; // TODO: only IAM and IPT are supported now, but it's not validated invDoc.ObjectVisibility.AllWorkFeatures = false; invDoc.ObjectVisibility.Sketches = false; invDoc.ObjectVisibility.Sketches3D = false; if (doc.DocumentType == DocumentTypeEnum.kAssemblyDocumentObject) { invDoc.ObjectVisibility.WeldmentSymbols = false; } string fileNameLarge = "thumbnail-large.png"; string filePathLarge = Path.Combine(Directory.GetCurrentDirectory(), fileNameLarge); inventorApplication.DisplayOptions.Show3DIndicator = false; Camera cam = inventorApplication.TransientObjects.CreateCamera(); cam.SceneObject = invDoc.ComponentDefinition; cam.ViewOrientationType = ViewOrientationTypeEnum.kIsoTopRightViewOrientation; cam.Fit(); cam.ApplyWithoutTransition(); Color backgroundColor = inventorApplication.TransientObjects.CreateColor(0xEC, 0xEC, 0xEC, 0.0); // hardcoded. Make as a parameter // generate image as twice larger size, and then downsample it (antialiasing) cam.SaveAsBitmap(filePathLarge, ThumbnailSize * 2, ThumbnailSize * 2, backgroundColor, backgroundColor); // based on https://stackoverflow.com/a/24199315 using (var image = Image.FromFile(filePathLarge)) using (var destImage = new Bitmap(ThumbnailSize, ThumbnailSize)) { destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); var destRect = new Rectangle(0, 0, ThumbnailSize, ThumbnailSize); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } } string fileName = "thumbnail.png"; string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName); destImage.Save(filePath); LogTrace($"Saved thumbnail as {filePath}"); } System.IO.File.Delete(filePathLarge); } catch (Exception e) { LogError("Processing failed. " + e.ToString()); } }
//update the bitmap according to select mode and settings private void updateBitmap() { if (m_inventorApplication == null) { return; } //prepare the size and corner point switch (oSelectOption) { case SelectOptionsEnum.eApplication: //point and size are always same for application oCornerPt = oSelectParam_App.oCornerPt; oSize = oSelectParam_App.oSize; break; case SelectOptionsEnum.eDocument: //point and size are always same for document oCornerPt = oSelectParam_Doc.oCornerPt; oSize = oSelectParam_Doc.oSize; break; case SelectOptionsEnum.eWindow: if (!(oHasSelectWindow)) { //need to select by user. so set preview to warning PictureBox1.Image = GetWarningBitmap(); return; } else { oCornerPt = oSelectParam_Win.oCornerPt; oSize = oSelectParam_Win.oSize; } break; } System.Drawing.Color oBgColor = System.Drawing.Color.Empty; //first priority is background color switch (ComboBoxBG.SelectedIndex) { case -1: { oResultBitmap = createRawBitmap(oSize, oCornerPt); if (m_inventorApplication.ActiveDocument == null) { return; } //normal background color Inventor.Color oInvBGColor = null; if ((m_inventorApplication.ActiveDocument) is DrawingDocument) { DrawingDocument oDrawDoc = (DrawingDocument)m_inventorApplication.ActiveDocument; oInvBGColor = oDrawDoc.SheetSettings.SheetColor; } else { oInvBGColor = m_inventorApplication.ActiveColorScheme.ScreenColor; } oBgColor = System.Drawing.Color.FromArgb(oInvBGColor.Red, oInvBGColor.Green, oInvBGColor.Blue); break; } case 0: // normal { oResultBitmap = createRawBitmap(oSize, oCornerPt); if (m_inventorApplication.ActiveDocument == null) { return; } //normal background color Inventor.Color oInvBGColor = null; if ((m_inventorApplication.ActiveDocument) is DrawingDocument) { DrawingDocument oDrawDoc = (DrawingDocument)m_inventorApplication.ActiveDocument; oInvBGColor = oDrawDoc.SheetSettings.SheetColor; } else { oInvBGColor = m_inventorApplication.ActiveColorScheme.ScreenColor; } oBgColor = System.Drawing.Color.FromArgb(oInvBGColor.Red, oInvBGColor.Green, oInvBGColor.Blue); break; } case 1: //white Background { oResultBitmap = getBmpByForceBGColor(System.Drawing.Color.White); oBgColor = System.Drawing.Color.White; break; } } //second priority is forecolor or grayscale switch (ComboBoxFG.SelectedIndex) { case 0: switch (ComboBoxGray.SelectedIndex) { case 0: //do nothing break; case 1: //gray oResultBitmap = ConvertToGrayscale(oResultBitmap, oBgColor, false, System.Drawing.Color.Empty); break; } break; case 1: //black oResultBitmap = ConvertToGrayscale(oResultBitmap, oBgColor, true, System.Drawing.Color.Black); break; } //set image to picturebox if (oResultBitmap != null) { PictureBox1.Image = oResultBitmap; //store the bitmaps for the current mode switch (oSelectOption) { case SelectOptionsEnum.eApplication: oBitmap_App = oResultBitmap; break; case SelectOptionsEnum.eDocument: oBitmap_Doc = oResultBitmap; break; case SelectOptionsEnum.eWindow: oBitmap_Win = oResultBitmap; break; } } }
private static string ColorString(Color color, StreamWriter file) { return("(" + color.Red + " " + color.Green + " " + color.Blue + " " + color.Opacity + "),"); }